package cmd import ( "fmt" "net/http" "git.towk2.me/towk/makeshift/pkg/client" "github.com/rs/zerolog/log" "github.com/spf13/cobra" ) var deleteCmd = &cobra.Command{ Use: "delete", Example: ` # set up environment export MAKESHIFT_HOST=http://localhost:5050 export MAKESHIFT_PATH=test # delete a file or directory (cannot delete root) makeshift delete -p help.txt makeshift delete --host http://localhost:5555 --path templates `, Short: "Delete files and directories", PersistentPreRun: func(cmd *cobra.Command, args []string) { setenv(cmd, "host", "MAKESHIFT_HOST") setenv(cmd, "path", "MAKESHIFT_PATH") setenv(cmd, "cacert", "MAKESHIFT_CACERT") }, Run: func(cmd *cobra.Command, args []string) { var ( host, _ = cmd.Flags().GetString("host") paths, _ = cmd.Flags().GetStringSlice("path") cacertPath, _ = cmd.Flags().GetString("cacert") c = client.New(host) res *http.Response query string err error ) log.Debug(). Str("host", host). Str("cacert", cacertPath). Send() if cacertPath != "" { c.LoadCertificateFromPath(cacertPath) } for _, path := range paths { if path == "" { log.Warn().Msg("skipping empty path") continue } query = fmt.Sprintf("/delete/%s?", path) res, _, err = c.MakeRequest(client.HTTPEnvelope{ Path: query, Method: http.MethodDelete, }) handleResponseError(res, host, query, err) } }, } var deleteProfilesCmd = &cobra.Command{ Use: "profiles", Example: ` # delete profile(s) by its ID makeshift delete profiles kubernetes slurm compute `, Args: cobra.MinimumNArgs(1), Short: "Delete profile(s)", Run: func(cmd *cobra.Command, args []string) { var ( host, _ = cmd.Flags().GetString("host") cacertPath, _ = cmd.Flags().GetString("cacert") c = client.New(host) res *http.Response query string err error ) log.Debug(). Str("host", host). Str("cacert", cacertPath). Send() if cacertPath != "" { c.LoadCertificateFromPath(cacertPath) } for _, profileID := range args { if profileID == "default" { log.Warn().Msg("cannot delete the default profile") continue } query = fmt.Sprintf("/profiles/%s", profileID) res, _, err = c.MakeRequest(client.HTTPEnvelope{ Path: query, Method: http.MethodDelete, }) handleResponseError(res, host, query, err) } }, } var deletePluginsCmd = &cobra.Command{ Use: "plugins", Example: ` # delete plugin(s) by name makeshift delete plugins weather slurm user `, Args: cobra.MinimumNArgs(1), Short: "Delete plugin(s)", Run: func(cmd *cobra.Command, args []string) { var ( host, _ = cmd.Flags().GetString("host") cacertPath, _ = cmd.Flags().GetString("cacert") c = client.New(host) res *http.Response query string err error ) log.Debug(). Str("host", host). Str("cacert", cacertPath). Send() if cacertPath != "" { c.LoadCertificateFromPath(cacertPath) } for _, pluginName := range args { query = fmt.Sprintf("/plugins/%s", pluginName) res, _, err = c.MakeRequest(client.HTTPEnvelope{ Path: query, Method: http.MethodDelete, }) handleResponseError(res, host, query, err) } }, } func init() { deleteCmd.PersistentFlags().String("host", "http://localhost:5050", "Set the makeshift server host (can be set with MAKESHIFT_HOST)") deleteCmd.PersistentFlags().String("cacert", "", "Set the CA certificate path to load") deleteCmd.Flags().StringSliceP("path", "p", []string{}, "Set the paths to delete files and directories") deleteCmd.AddCommand(deleteProfilesCmd, deletePluginsCmd) rootCmd.AddCommand(deleteCmd) }