Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ This project includes code licensed under the following terms:

----------
Module: code.gitea.io/sdk/gitea
Version: v0.23.2
Version: v0.24.1
License: MIT
License URL: https://gitea.com/gitea/go-sdk/src/tag/gitea/v0.23.2/gitea/LICENSE
License URL: https://gitea.com/gitea/go-sdk/src/tag/gitea/v0.24.1/gitea/LICENSE

----------
Module: dario.cat/mergo
Expand All @@ -17,9 +17,9 @@ License URL: https://github.com/imdario/mergo/blob/v1.0.2/LICENSE

----------
Module: github.com/42wim/httpsig
Version: v1.2.3
Version: v1.2.4
License: BSD-3-Clause
License URL: https://github.com/42wim/httpsig/blob/v1.2.3/LICENSE
License URL: https://github.com/42wim/httpsig/blob/v1.2.4/LICENSE

----------
Module: github.com/Masterminds/semver/v3
Expand All @@ -29,9 +29,9 @@ License URL: https://github.com/Masterminds/semver/blob/v3.4.0/LICENSE.txt

----------
Module: github.com/ProtonMail/go-crypto
Version: v1.3.0
Version: v1.4.0
License: BSD-3-Clause
License URL: https://github.com/ProtonMail/go-crypto/blob/v1.3.0/LICENSE
License URL: https://github.com/ProtonMail/go-crypto/blob/v1.4.0/LICENSE

----------
Module: github.com/beorn7/perks/quantile
Expand Down Expand Up @@ -125,9 +125,9 @@ License URL: https://github.com/go-git/go-billy/blob/v5.8.0/LICENSE

----------
Module: github.com/go-git/go-git/v5
Version: v5.17.0
Version: v5.17.2
License: Apache-2.0
License URL: https://github.com/go-git/go-git/blob/v5.17.0/LICENSE
License URL: https://github.com/go-git/go-git/blob/v5.17.2/LICENSE

----------
Module: github.com/go-logr/logr
Expand Down Expand Up @@ -185,9 +185,9 @@ License URL: https://github.com/jbenet/go-context/blob/d14ea06fba99/LICENSE

----------
Module: github.com/jedib0t/go-pretty/v6
Version: v6.7.8
Version: v6.7.9
License: MIT
License URL: https://github.com/jedib0t/go-pretty/blob/v6.7.8/LICENSE
License URL: https://github.com/jedib0t/go-pretty/blob/v6.7.9/LICENSE

----------
Module: github.com/json-iterator/go
Expand Down Expand Up @@ -395,9 +395,9 @@ License URL: https://cs.opensource.google/go/x/net/+/v0.52.0:LICENSE

----------
Module: golang.org/x/oauth2
Version: v0.35.0
Version: v0.36.0
License: BSD-3-Clause
License URL: https://cs.opensource.google/go/x/oauth2/+/v0.35.0:LICENSE
License URL: https://cs.opensource.google/go/x/oauth2/+/v0.36.0:LICENSE

----------
Module: golang.org/x/sync/errgroup
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func AddCreateCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
Long: `Create codesphere resources like workspaces.`,
},
}
rootCmd.AddCommand(create.cmd)
AddCmd(rootCmd, create.cmd)

// Add child commands here
AddCreateWorkspaceCmd(create.cmd, opts)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/create_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func AddCreateWorkspaceCmd(create *cobra.Command, opts *GlobalOptions) {
workspace.Opts.Baseimage = workspace.cmd.Flags().String("base-image", "", "Base image to use for the workspace, e.g. 'ubuntu-24.04'")
workspace.Opts.PublicDevDomain = workspace.cmd.Flags().Bool("public-dev-domain", false, "Whether to create enable a public development domain (defaults to the public api default)")

create.AddCommand(workspace.cmd)
AddCmd(create, workspace.cmd)
workspace.cmd.RunE = workspace.RunE
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func AddDeleteCmd(rootCmd *cobra.Command, opt *GlobalOptions) {
Long: `Delete Codesphere resources, e.g. workspaces.`,
},
}
rootCmd.AddCommand(delete.cmd)
AddCmd(rootCmd, delete.cmd)

// Add child commands here
AddDeleteWorkspaceCmd(delete.cmd, opt)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/delete_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func AddDeleteWorkspaceCmd(delete *cobra.Command, opts *GlobalOptions) {
Prompt: &io.Prompt{},
}
workspace.Opts.Confirmed = workspace.cmd.Flags().Bool("yes", false, "Confirm deletion of workspace")
delete.AddCommand(workspace.cmd)
AddCmd(delete, workspace.cmd)
workspace.cmd.RunE = workspace.RunE
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func AddExecCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
}
exec.Opts.EnvVar = exec.cmd.Flags().StringArrayP("env", "e", []string{}, "Additional environment variables to pass to the command in the form key=val")
exec.Opts.WorkDir = exec.cmd.Flags().StringP("workdir", "d", "", "Working directory for the command")
rootCmd.AddCommand(exec.cmd)
AddCmd(rootCmd, exec.cmd)
exec.cmd.RunE = exec.RunE
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func AddGenerateCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
generate.cmd.PersistentFlags().BoolVarP(&generate.Opts.Force, "force", "f", false, "Overwrite any files if existing")
generate.cmd.PersistentFlags().StringVar(&generate.Opts.RepoRoot, "reporoot", "./workspace-repo", "root directory of the workspace repository to export. Will be used to clone the repository if it doesn't exist.")

rootCmd.AddCommand(generate.cmd)
AddCmd(rootCmd, generate.cmd)

AddGenerateDockerCmd(generate.cmd, generate.Opts)
AddGenerateKubernetesCmd(generate.cmd, generate.Opts)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/generate_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func AddGenerateDockerCmd(generate *cobra.Command, opts *GenerateOpts) {
docker.cmd.Flags().StringVarP(&docker.Opts.BaseImage, "baseimage", "b", "", "Base image for the docker")
docker.cmd.Flags().StringArrayVarP(&docker.Opts.Envs, "env", "e", []string{}, "Env vars to put into generated artifacts")

generate.AddCommand(docker.cmd)
AddCmd(generate, docker.cmd)
docker.cmd.RunE = docker.RunE
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/generate_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func AddGenerateImagesCmd(generate *cobra.Command, opts *GenerateOpts) {
images.cmd.Flags().StringVarP(&images.Opts.Registry, "registry", "r", "", "Registry to push the resulting images to")
images.cmd.Flags().StringVarP(&images.Opts.ImagePrefix, "imagePrefix", "p", "", "Image prefix to use for the exported images")

generate.AddCommand(images.cmd)
AddCmd(generate, images.cmd)
images.cmd.RunE = images.RunE
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/generate_kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func AddGenerateKubernetesCmd(generate *cobra.Command, opts *GenerateOpts) {
kubernetes.cmd.Flags().StringVar(&kubernetes.Opts.Hostname, "hostname", "localhost", "hostname for the ingress to match")
kubernetes.cmd.Flags().StringVar(&kubernetes.Opts.IngressClass, "ingressClass", "nginx", "ingress class for the ingress resource")

generate.AddCommand(kubernetes.cmd)
AddCmd(generate, kubernetes.cmd)
kubernetes.cmd.RunE = kubernetes.RunE
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func AddGitCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
like pulling or switching to a specific branch.`),
},
}
rootCmd.AddCommand(git.cmd)
AddCmd(rootCmd, git.cmd)

// Add child commands here
AddGitPullCmd(git.cmd, opts)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/git_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func AddGitPullCmd(git *cobra.Command, opts *GlobalOptions) {
Opts: GitPullOpts{GlobalOptions: opts},
}

git.AddCommand(pull.cmd)
AddCmd(git, pull.cmd)
pull.Opts.Branch = pull.cmd.Flags().String("branch", "", "Branch to pull")
pull.Opts.Remote = pull.cmd.Flags().String("remote", "", "Remote to pull from")
pull.cmd.RunE = pull.RunE
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ func AddGoCmd(rootCmd *cobra.Command) {
goCmd := GoCmd{
cmd: &cobra.Command{Hidden: true, Use: "go"},
}
rootCmd.AddCommand(goCmd.cmd)
AddCmd(rootCmd, goCmd.cmd)
goCmd.cmd.RunE = goCmd.RunE
}
2 changes: 1 addition & 1 deletion cli/cmd/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ func AddLicensesCmd(rootCmd *cobra.Command) {
Long: `Print information about the Codesphere CLI license and open source licenses of dependencies.`,
},
}
rootCmd.AddCommand(licenses.cmd)
AddCmd(rootCmd, licenses.cmd)
licenses.cmd.RunE = licenses.RunE
}
2 changes: 1 addition & 1 deletion cli/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func AddListCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
return nil
}

rootCmd.AddCommand(l.cmd)
AddCmd(rootCmd, l.cmd)
addListWorkspacesCmd(l.cmd, listOpts)
AddListBaseimagesCmd(l.cmd, listOpts)
addListTeamsCmd(l.cmd, listOpts)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/list_baseimages.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func AddListBaseimagesCmd(p *cobra.Command, opts *ListOptions) {
Opts: opts,
}
l.cmd.RunE = l.RunE
p.AddCommand(l.cmd)
AddCmd(p, l.cmd)
}

func (l *ListBaseimagesCmd) RunE(_ *cobra.Command, args []string) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/list_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ func AddListPlansCmd(list *cobra.Command, opts *ListOptions) {
},
Opts: opts,
}
list.AddCommand(plans.cmd)
AddCmd(list, plans.cmd)
plans.cmd.RunE = plans.RunE
}
2 changes: 1 addition & 1 deletion cli/cmd/list_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func addListTeamsCmd(p *cobra.Command, opts *ListOptions) {
opts: opts,
}
l.cmd.RunE = l.RunE
p.AddCommand(l.cmd)
AddCmd(p, l.cmd)
}

func (l *ListTeamsCmd) RunE(_ *cobra.Command, args []string) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/list_workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func addListWorkspacesCmd(p *cobra.Command, opts *ListOptions) {
Opts: opts,
}
l.cmd.RunE = l.RunE
p.AddCommand(l.cmd)
AddCmd(p, l.cmd)
}

func (l *ListWorkspacesCmd) RunE(_ *cobra.Command, args []string) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func AddLogCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
}
logCmd.cmd.RunE = logCmd.RunE
logCmd.parseLogCmdFlags()
rootCmd.AddCommand(logCmd.cmd)
AddCmd(rootCmd, logCmd.cmd)
}

func (logCmd *LogCmd) parseLogCmdFlags() {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,6 @@ func AddMonitorCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
monitor.Opts.Forward = monitor.Cmd.Flags().String("forward", "", "Forward healthcheck requests to application health endpoint")
monitor.Opts.InsecureSkipVerify = monitor.Cmd.Flags().Bool("insecure-skip-verify", false, "Skip TLS validation (only relevant for --forward option when healthcheck is exposed as HTTPS endpoint with custom certificate)")
monitor.Opts.CaCertFile = monitor.Cmd.Flags().String("ca-cert-file", "", "TLS CA certificate (only relevant for --forward option when healthcheck is exposed as HTTPS enpoint with custom certificate)")
rootCmd.AddCommand(monitor.Cmd)
AddCmd(rootCmd, monitor.Cmd)
monitor.Cmd.RunE = monitor.RunE
}
2 changes: 1 addition & 1 deletion cli/cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func AddOpenCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
Long: `Open the Codesphere IDE.`,
},
}
rootCmd.AddCommand(open.cmd)
AddCmd(rootCmd, open.cmd)
open.cmd.RunE = open.RunE
AddOpenWorkspaceCmd(open.cmd, opts)
}
2 changes: 1 addition & 1 deletion cli/cmd/open_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func AddOpenWorkspaceCmd(open *cobra.Command, opts *GlobalOptions) {
},
Opts: opts,
}
open.AddCommand(workspace.cmd)
AddCmd(open, workspace.cmd)
workspace.cmd.RunE = workspace.RunE
}

Expand Down
10 changes: 10 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,22 @@ func (o GlobalOptions) GetWorkspaceId() (int, error) {
return wsId, nil
}

// AddCmd adds a command, inheriting the parent's Args validator if not explicitly set.
// Individual commands that need different argument rules can override this by setting their own Args validator.
func AddCmd(parent *cobra.Command, cmd *cobra.Command) {
if cmd.Args == nil {
cmd.Args = parent.Args
}
parent.AddCommand(cmd)
}

func GetRootCmd() *cobra.Command {
var rootCmd = &cobra.Command{
Use: "cs",
Short: "The Codesphere CLI",
Long: `Manage and debug resources deployed in Codesphere via command line.`,
DisableAutoGenTag: true,
Args: cobra.NoArgs,
}

opts := GlobalOptions{Env: cs.NewEnv()}
Expand Down
65 changes: 65 additions & 0 deletions cli/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0

package cmd_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"

"github.com/codesphere-cloud/cs-go/cli/cmd"
)

func findCommand(root *cobra.Command, path ...string) *cobra.Command {
current := root
for _, p := range path {
next, _, err := current.Find([]string{p})
if err != nil || next == nil {
return nil
}
current = next
}

return current
}

var _ = Describe("RootCmd", func() {
It("rejects positional args for commands configured with no positional args", func() {
rootCmd := cmd.GetRootCmd()
rootCmd.SilenceErrors = true
rootCmd.SilenceUsage = true

licensesCmd := findCommand(rootCmd, "licenses")
Expect(licensesCmd).NotTo(BeNil())

rootCmd.SetArgs([]string{"licenses", "extra"})
err := rootCmd.Execute()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("unknown command \"extra\""))
})

It("allows positional args for commands explicitly defining them", func() {
rootCmd := cmd.GetRootCmd()
rootCmd.SilenceErrors = true
rootCmd.SilenceUsage = true

createWorkspaceCmd := findCommand(rootCmd, "create", "workspace")
Expect(createWorkspaceCmd).NotTo(BeNil())

executed := false
capturedArgs := []string{}
createWorkspaceCmd.RunE = func(_ *cobra.Command, args []string) error {
executed = true
capturedArgs = args
return nil
}

rootCmd.SetArgs([]string{"create", "workspace", "my-workspace"})
err := rootCmd.Execute()

Expect(err).NotTo(HaveOccurred())
Expect(executed).To(BeTrue())
Expect(capturedArgs).To(Equal([]string{"my-workspace"}))
})
})
2 changes: 1 addition & 1 deletion cli/cmd/set_env_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func AddSetEnvVarCmd(p *cobra.Command, opts *GlobalOptions) {
}
l.cmd.RunE = l.RunE
l.parseFlags()
p.AddCommand(l.cmd)
AddCmd(p, l.cmd)
}

func (l *SetEnvVarCmd) parseFlags() {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ func AddStartCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
Long: `Start pipeline of a workspace using the pipeline subcommand`,
},
}
rootCmd.AddCommand(start.cmd)
AddCmd(rootCmd, start.cmd)
AddStartPipelineCmd(start.cmd, opts)
}
2 changes: 1 addition & 1 deletion cli/cmd/start_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func AddStartPipelineCmd(start *cobra.Command, opts *GlobalOptions) {

pipeline.Opts.Timeout = pipeline.cmd.Flags().Duration("timeout", 30*time.Minute, "Time to wait per stage before stopping the command execution (e.g. 10m)")
pipeline.Opts.Profile = pipeline.cmd.Flags().StringP("profile", "p", "", "CI profile to use (e.g. 'prod' for the profile defined in 'ci.prod.yml'), defaults to the ci.yml profile")
start.AddCommand(pipeline.cmd)
AddCmd(start, pipeline.cmd)

pipeline.cmd.RunE = pipeline.RunE
}
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func AddSyncCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
Long: io.Long(`Synchronize Codesphere resources, like infrastructure required to run services.`),
},
}
rootCmd.AddCommand(sync.cmd)
AddCmd(rootCmd, sync.cmd)

AddSyncLandscapeCmd(sync.cmd, opts)
}
2 changes: 1 addition & 1 deletion cli/cmd/sync_landscape.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func AddSyncLandscapeCmd(sync *cobra.Command, opts *GlobalOptions) {

workspace.cmd.RunE = workspace.RunE

sync.AddCommand(workspace.cmd)
AddCmd(sync, workspace.cmd)
}

func (c *SyncLandscapeCmd) SyncLandscape(client Client, wsId int) error {
Expand Down
Loading
Loading