diff --git a/cmd/delete.go b/cmd/delete.go new file mode 100644 index 0000000..85dbd71 --- /dev/null +++ b/cmd/delete.go @@ -0,0 +1,123 @@ +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") + }, + Run: func(cmd *cobra.Command, args []string) { + var ( + host, _ = cmd.Flags().GetString("host") + paths, _ = cmd.Flags().GetStringSlice("path") + + c = client.New(host) + res *http.Response + query string + err error + ) + 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") + + c = client.New(host) + res *http.Response + query string + err error + ) + + 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") + + c = client.New(host) + res *http.Response + query string + err error + ) + + 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.Flags().StringSliceP("path", "p", []string{}, "Set the paths to delete files and directories") + deleteCmd.AddCommand(deleteProfilesCmd, deletePluginsCmd) + + rootCmd.AddCommand(deleteCmd) +}