forked from codesphere-cloud/cs-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.go
More file actions
107 lines (93 loc) · 2.55 KB
/
root.go
File metadata and controls
107 lines (93 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"errors"
"os"
"github.com/codesphere-cloud/cs-go/pkg/cs"
"github.com/spf13/cobra"
)
type GlobalOptions struct {
ApiUrl *string
TeamId *int
WorkspaceId *int
Env Env
Verbose *bool
}
type Env interface {
GetApiToken() (string, error)
GetTeamId() (int, error)
GetWorkspaceId() (int, error)
GetApiUrl() string
}
func (o GlobalOptions) GetApiUrl() string {
if o.ApiUrl != nil && *o.ApiUrl != "" {
return *o.ApiUrl
}
return o.Env.GetApiUrl()
}
func (o GlobalOptions) GetTeamId() (int, error) {
if o.TeamId != nil && *o.TeamId != -1 {
return *o.TeamId, nil
}
wsId, err := o.Env.GetTeamId()
if err != nil {
return -1, err
}
if wsId < 0 {
return -1, errors.New("team ID not set, use -t or CS_TEAM_ID to set it")
}
return wsId, nil
}
func (o GlobalOptions) GetWorkspaceId() (int, error) {
if o.WorkspaceId != nil && *o.WorkspaceId != -1 {
return *o.WorkspaceId, nil
}
wsId, err := o.Env.GetWorkspaceId()
if err != nil {
return -1, err
}
if wsId < 0 {
return -1, errors.New("workspace ID not set, use -w or CS_WORKSPACE_ID to set it")
}
return wsId, nil
}
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,
}
opts := GlobalOptions{Env: cs.NewEnv()}
opts.ApiUrl = rootCmd.PersistentFlags().StringP("api", "a", "", "URL of Codesphere API (can also be CS_API)")
opts.TeamId = rootCmd.PersistentFlags().IntP("team", "t", -1, "Team ID (relevant for some commands, can also be CS_TEAM_ID)")
opts.WorkspaceId = rootCmd.PersistentFlags().IntP("workspace", "w", -1, "Workspace ID (relevant for some commands, can also be CS_WORKSPACE_ID)")
opts.Verbose = rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Verbose output")
AddExecCmd(rootCmd, opts)
AddLogCmd(rootCmd, opts)
AddListCmd(rootCmd, opts)
AddSetEnvVarCmd(rootCmd, opts)
AddVersionCmd(rootCmd)
AddLicensesCmd(rootCmd)
AddOpenCmd(rootCmd, opts)
AddGenerateCmd(rootCmd, opts)
AddCreateCmd(rootCmd, opts)
AddDeleteCmd(rootCmd, opts)
AddMonitorCmd(rootCmd, opts)
AddStartCmd(rootCmd, opts)
AddGitCmd(rootCmd, opts)
AddSyncCmd(rootCmd, &opts)
AddUpdateCmd(rootCmd)
AddGoCmd(rootCmd)
AddWakeUpCmd(rootCmd, opts)
AddCurlCmd(rootCmd, opts)
AddDeployCmd(rootCmd, opts)
return rootCmd
}
func Execute() {
err := GetRootCmd().Execute()
if err != nil {
os.Exit(1)
}
}