-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add some resource commands (#2)
- Loading branch information
Showing
12 changed files
with
564 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package restart | ||
|
||
import ( | ||
"github.com/fioncat/kubewrap/cmd" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func CompletionFunc(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
switch len(args) { | ||
case 0: | ||
return cmd.CompleteResource(c, toComplete) | ||
} | ||
|
||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package restart | ||
|
||
import ( | ||
"github.com/fioncat/kubewrap/cmd" | ||
"github.com/fioncat/kubewrap/pkg/term" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func New() *cobra.Command { | ||
var opts Options | ||
c := &cobra.Command{ | ||
Use: "restart <QUERY>", | ||
Short: "Restart a resource", | ||
Args: cobra.ExactArgs(1), | ||
|
||
ValidArgsFunction: CompletionFunc, | ||
} | ||
return cmd.Build(c, &opts) | ||
} | ||
|
||
type Options struct { | ||
query string | ||
} | ||
|
||
func (o *Options) Validate(_ *cobra.Command, args []string) error { | ||
o.query = args[0] | ||
return nil | ||
} | ||
|
||
func (o *Options) Run(cmdctx *cmd.Context) error { | ||
r, err := cmd.SelectResource(cmdctx.Kubectl, o.query) | ||
if err != nil { | ||
return err | ||
} | ||
err = cmdctx.Kubectl.RolloutRestart(r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
term.PrintHint("Restarted %v", r) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package scale | ||
|
||
import ( | ||
"github.com/fioncat/kubewrap/cmd" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func CompletionFunc(c *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
switch len(args) { | ||
case 0: | ||
return cmd.CompleteResource(c, toComplete) | ||
} | ||
|
||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package scale | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/fioncat/kubewrap/cmd" | ||
"github.com/fioncat/kubewrap/pkg/term" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func New() *cobra.Command { | ||
var opts Options | ||
c := &cobra.Command{ | ||
Use: "scale <QUERY> <REPLICAS>", | ||
Short: "Scale the replicas of a resource", | ||
Args: cobra.ExactArgs(2), | ||
|
||
ValidArgsFunction: CompletionFunc, | ||
} | ||
return cmd.Build(c, &opts) | ||
} | ||
|
||
type Options struct { | ||
query string | ||
replicas int | ||
} | ||
|
||
func (o *Options) Validate(_ *cobra.Command, args []string) error { | ||
o.query = args[0] | ||
|
||
var err error | ||
o.replicas, err = strconv.Atoi(args[1]) | ||
if err != nil { | ||
return fmt.Errorf("replicas must be an integer: %w", err) | ||
} | ||
if o.replicas < 0 { | ||
return errors.New("replicas must be greater than or equal to 0") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *Options) Run(cmdctx *cmd.Context) error { | ||
r, err := cmd.SelectResource(cmdctx.Kubectl, o.query) | ||
if err != nil { | ||
return err | ||
} | ||
err = cmdctx.Kubectl.Scale(r, o.replicas) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
term.PrintHint("Scaled %v to %d", r, o.replicas) | ||
return nil | ||
} |
Oops, something went wrong.