Skip to content

Commit

Permalink
Move GitUsername and GitToken out of GlobalFlags to separate GitCrede…
Browse files Browse the repository at this point in the history
…ntialsFlags struct
  • Loading branch information
janekbaraniewski committed Oct 8, 2024
1 parent d4b357f commit 3db91a9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
18 changes: 13 additions & 5 deletions cmd/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ type GlobalFlags struct {

DevPodHome string

GitUsername string
GitToken string
UID string
UID string
}

// SetGlobalFlags applies the global flags
Expand All @@ -32,8 +30,6 @@ func SetGlobalFlags(flags *flag.FlagSet) *GlobalFlags {
flags.BoolVar(&globalFlags.Debug, "debug", false, "Prints the stack trace if an error occurs")
flags.BoolVar(&globalFlags.Silent, "silent", false, "Run in silent mode and prevents any devpod log output except panics & fatals")

flags.StringVar(&globalFlags.GitUsername, "git-username", "", "The username to use for git operations")
flags.StringVar(&globalFlags.GitToken, "git-token", "", "The token to use for git operations")
_ = flags.MarkHidden("git-username")
_ = flags.MarkHidden("git-token")
flags.StringVar(&globalFlags.UID, "uid", "", "Set UID for workspace")
Expand All @@ -43,3 +39,15 @@ func SetGlobalFlags(flags *flag.FlagSet) *GlobalFlags {
_ = flags.MarkHidden("agent-dir")
return globalFlags
}

type GitCredentialsFlags struct {
GitUsername string
GitToken string
}

func SetGitCredentialsFlags(flags *flag.FlagSet, o *GitCredentialsFlags) {
flags.StringVar(&o.GitUsername, "git-username", "", "The username to use for git operations")
flags.StringVar(&o.GitToken, "git-token", "", "The token to use for git operations")
_ = flags.MarkHidden("git-username")
_ = flags.MarkHidden("git-token")
}
7 changes: 5 additions & 2 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
// SSHCmd holds the ssh cmd flags
type SSHCmd struct {
*flags.GlobalFlags
*flags.GitCredentialsFlags

ForwardPortsTimeout string
ForwardPorts []string
Expand All @@ -55,9 +56,9 @@ type SSHCmd struct {
}

// NewSSHCmd creates a new ssh command
func NewSSHCmd(flags *flags.GlobalFlags) *cobra.Command {
func NewSSHCmd(f *flags.GlobalFlags) *cobra.Command {
cmd := &SSHCmd{
GlobalFlags: flags,
GlobalFlags: f,
}
sshCmd := &cobra.Command{
Use: "ssh",
Expand Down Expand Up @@ -85,6 +86,7 @@ func NewSSHCmd(flags *flags.GlobalFlags) *cobra.Command {
},
}

flags.SetGitCredentialsFlags(sshCmd.Flags(), cmd.GitCredentialsFlags)
sshCmd.Flags().StringArrayVarP(&cmd.ForwardPorts, "forward-ports", "L", []string{}, "Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be forwarded to the given host and port, or Unix socket, on the remote side.")
sshCmd.Flags().StringArrayVarP(&cmd.ReverseForwardPorts, "reverse-forward-ports", "R", []string{}, "Specifies that connections to the given TCP port or Unix socket on the local (client) host are to be reverse forwarded to the given host and port, or Unix socket, on the remote side.")
sshCmd.Flags().StringVar(&cmd.ForwardPortsTimeout, "forward-ports-timeout", "", "Specifies the timeout after which the command should terminate when the ports are unused.")
Expand All @@ -96,6 +98,7 @@ func NewSSHCmd(flags *flags.GlobalFlags) *cobra.Command {
sshCmd.Flags().BoolVar(&cmd.GPGAgentForwarding, "gpg-agent-forwarding", false, "If true forward the local gpg-agent to the remote machine")
sshCmd.Flags().BoolVar(&cmd.Stdio, "stdio", false, "If true will tunnel connection through stdout and stdin")
sshCmd.Flags().BoolVar(&cmd.StartServices, "start-services", true, "If false will not start any port-forwarding or git / docker credentials helper")

return sshCmd
}

Expand Down
9 changes: 3 additions & 6 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ type UpCmd struct {
}

// NewUpCmd creates a new up command
func NewUpCmd(flags *flags.GlobalFlags) *cobra.Command {
func NewUpCmd(f *flags.GlobalFlags) *cobra.Command {
cmd := &UpCmd{
GlobalFlags: flags,
GlobalFlags: f,
}
upCmd := &cobra.Command{
Use: "up",
Expand Down Expand Up @@ -147,7 +147,7 @@ func NewUpCmd(flags *flags.GlobalFlags) *cobra.Command {
return cmd.Run(ctx, devPodConfig, client, logger)
},
}

flags.SetGitCredentialsFlags(upCmd.Flags(), cmd.GitCredentialsFlags)
upCmd.Flags().BoolVar(&cmd.ConfigureSSH, "configure-ssh", true, "If true will configure the ssh config to include the DevPod workspace")
upCmd.Flags().BoolVar(&cmd.GPGAgentForwarding, "gpg-agent-forwarding", false, "If true forward the local gpg-agent to the DevPod workspace")
upCmd.Flags().StringVar(&cmd.SSHConfigPath, "ssh-config", "", "The path to the ssh config to modify, if empty will use ~/.ssh/config")
Expand Down Expand Up @@ -476,9 +476,6 @@ func (cmd *UpCmd) devPodUpMachine(
return nil, err
}

cmd.CLIOptions.GitOverrideUsername = cmd.GitUsername
cmd.CLIOptions.GitOverrideToken = cmd.GitToken

// compress info
workspaceInfo, wInfo, err := client.AgentInfo(cmd.CLIOptions)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/devcontainer/crane/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ func PullConfigFromSource(workspaceInfo *provider2.AgentWorkspaceInfo, options *
WithArg(EnvironmentCrane).
WithArg(options.EnvironmentTemplate)

if options.GitOverrideUsername != "" && options.GitOverrideToken != "" {
command = command.WithFlag("--git-username", options.GitOverrideUsername).
WithFlag("--git-token", options.GitOverrideToken)
if options.GitUsername != "" && options.GitToken != "" {
command = command.WithFlag("--git-username", options.GitUsername).
WithFlag("--git-token", options.GitToken)
}
data, err = command.Run()
case options.DevContainerSource != "":
Expand Down
4 changes: 2 additions & 2 deletions pkg/provider/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
"time"

"github.com/loft-sh/devpod/cmd/flags"
"github.com/loft-sh/devpod/pkg/config"
devcontainerconfig "github.com/loft-sh/devpod/pkg/devcontainer/config"
"github.com/loft-sh/devpod/pkg/git"
Expand Down Expand Up @@ -182,6 +183,7 @@ type AgentWorkspaceInfo struct {
}

type CLIOptions struct {
*flags.GitCredentialsFlags
// up options
ID string `json:"id,omitempty"`
Source string `json:"source,omitempty"`
Expand All @@ -204,8 +206,6 @@ type CLIOptions struct {
GitCloneStrategy git.CloneStrategy `json:"gitCloneStrategy,omitempty"`
FallbackImage string `json:"fallbackImage,omitempty"`
GitSSHSigningKey string `json:"gitSshSigningKey,omitempty"`
GitOverrideUsername string `json:"gitUsername,omitempty"`
GitOverrideToken string `json:"gitToken,omitempty"`

// build options
Repository string `json:"repository,omitempty"`
Expand Down

0 comments on commit 3db91a9

Please sign in to comment.