diff --git a/README.md b/README.md index b323066..6b74890 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,8 @@ Usage: kmux restart TYPE NAME [flags] Flags: - -h, --help help for restart + -h, --help help for restart + -u, --user string User added in the restartedBy annotation (read from $KMUX_USER) ``` ### `image` diff --git a/cmd/restart.go b/cmd/restart.go index 4ed5f3c..1e1b162 100644 --- a/cmd/restart.go +++ b/cmd/restart.go @@ -3,12 +3,13 @@ package cmd import ( "context" "encoding/json" + "errors" "fmt" + "os" "strings" "time" "github.com/ViBiOh/kmux/pkg/client" - "github.com/ViBiOh/kmux/pkg/output" "github.com/ViBiOh/kmux/pkg/resource" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -16,6 +17,8 @@ import ( "k8s.io/apimachinery/pkg/types" ) +var user string + type restartPatch struct { Spec struct { Template struct { @@ -56,22 +59,26 @@ var restartCmd = &cobra.Command{ return nil, cobra.ShellCompDirectiveNoFileComp }, Args: cobra.MatchAll(cobra.ExactArgs(2), cobra.OnlyValidArgs), - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { resourceType := args[0] resourceName := args[1] + if len(user) == 0 { + return errors.New("--user is required") + } + ctx, cancel := context.WithCancel(cmd.Context()) defer cancel() var patch restartPatch patch.Spec.Template.Metadata.Annotations = map[string]string{ "kmux.vibioh.fr/restartedAt": time.Now().Format(time.RFC3339), + "kmux.vibioh.fr/restartedBy": user, } payload, err := json.Marshal(patch) if err != nil { - output.Err("", "marshal patch: %s", err) - return + return fmt.Errorf("marshal patch: %w", err) } clients.Execute(ctx, func(ctx context.Context, kube client.Kube) error { @@ -106,5 +113,13 @@ var restartCmd = &cobra.Command{ return fmt.Errorf("unhandled resource type `%s` for restart", resourceType) } }) + + return nil }, } + +func initRestart() { + flags := restartCmd.Flags() + + flags.StringVarP(&user, "user", "u", os.Getenv("KMUX_USER"), "User added in the restartedBy annotation (read from $KMUX_USER)") +} diff --git a/cmd/root.go b/cmd/root.go index 900b878..6c29c77 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -144,6 +144,8 @@ func init() { } rootCmd.AddCommand(versionCmd) + + initRestart() rootCmd.AddCommand(restartCmd) initImage() diff --git a/go.mod b/go.mod index 46cb75d..25cddbc 100644 --- a/go.mod +++ b/go.mod @@ -47,9 +47,9 @@ require ( github.com/subosito/gotenv v1.4.2 // indirect golang.org/x/net v0.10.0 // indirect golang.org/x/oauth2 v0.7.0 // indirect - golang.org/x/sys v0.10.0 // indirect + golang.org/x/sys v0.11.0 // indirect golang.org/x/term v0.8.0 // indirect - golang.org/x/text v0.11.0 // indirect + golang.org/x/text v0.12.0 // indirect golang.org/x/time v0.1.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.30.0 // indirect diff --git a/go.sum b/go.sum index 99abf17..ddef98d 100644 --- a/go.sum +++ b/go.sum @@ -381,8 +381,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= -golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= @@ -394,8 +394,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= -golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= +golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=