-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (120 loc) Β· 5.45 KB
/
main.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"encoding/json"
"fmt"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
"harness/auth"
"harness/defaults"
"harness/globals"
. "harness/share"
"harness/utils"
"os"
)
var Version = "Harness Commander CLI : Hack Week 24 π"
type UserDataLoad struct {
ApiKey string `json:"apiKey"`
AccountId string `json:"accountId"`
BaseURL string `json:"baseUrl"`
UserId string `json:"userId"`
}
var asciiArt = `
βββ βββ ββββββ βββββββ ββββ βββββββββββββββββββββββββββ βββββββ βββββββ ββββ ββββββββ ββββ ββββββ ββββ ββββββββββ βββββββββββββββ
βββ ββββββββββββββββββββββββ βββββββββββββββββββββββββββ ββββββββββββββββββββββ ββββββββββ ββββββββββββββββββ βββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββ βββββββββ ββββββββββββββββ βββ βββ βββββββββββββββββββββββββββββββββββββββ ββββββ βββββββββ ββββββββ
ββββββββββββββββββββββββββββββββββββββββ ββββββββββββββββ βββ βββ ββββββββββββββββββββββββββββββββββββββββββββββ βββββββββ ββββββββ
βββ ββββββ ββββββ ββββββ ββββββββββββββββββββββββββββββ ββββββββββββββββββββ βββ ββββββ βββ ββββββ ββββββ βββββββββββββββββββββββββ βββ
βββ ββββββ ββββββ ββββββ βββββββββββββββββββββββββββββ βββββββ βββββββ βββ ββββββ ββββββ ββββββ ββββββββββββ βββββββββββ βββ
Welcome to the Harness Commander, a utility to interact with the Harness Platform and seamlessly
deploy your applications.
`
type cliFnWrapper func(ctx *cli.Context) error
func init() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
cli.VersionPrinter = func(cCtx *cli.Context) {
fmt.Println(cCtx.App.Version)
}
}
func main() {
color.Set(color.FgBlue)
fmt.Println(asciiArt)
color.Unset()
app := &cli.App{
Name: "harness",
Version: Version,
Usage: "Welcome to Harness Commander, a utility to interact with Harness Platform to seamlessly deploy your applications.",
EnableBashCompletion: true,
Commands: []*cli.Command{
{
Name: "login",
Usage: "Authenticate with Harness",
Action: func(context *cli.Context) error {
return cliWrapper(func(context *cli.Context) error {
return auth.Login(context)
}, context)
},
},
{
Name: "init",
Usage: "Initialize Harness Commander CLI in the project",
Action: func(context *cli.Context) error {
if err := LoadCredentials(); err != nil {
fmt.Println(err, "\nPlease log in first.")
return auth.Login(context)
}
return cliWrapper(InitProject, context)
},
},
{
Name: "deploy",
Usage: "Deploy your project using Harness",
Action: func(context *cli.Context) error {
if err := LoadCredentials(); err != nil {
fmt.Println(err, "\nPlease log in first.")
return auth.Login(context)
}
return cliWrapper(DeployProject, context)
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func cliWrapper(fn cliFnWrapper, ctx *cli.Context) error {
if CliRequestData.Debug {
log.SetLevel(log.DebugLevel)
}
if CliRequestData.Json {
log.SetFormatter(&log.JSONFormatter{})
}
return fn(ctx)
}
func beforeAction(globalFlags []cli.Flag) {
altsrc.InitInputSourceWithContext(globalFlags, altsrc.NewYamlSourceFromFlagFunc("load"))
}
func LoadCredentials() error {
exactFilePath := utils.GetUserHomePath() + "/" + defaults.SECRETS_STORE_PATH
credsJson, err := os.ReadFile(exactFilePath)
if err != nil {
return fmt.Errorf("error reading creds file: %v", err)
}
var UserData UserDataLoad
err = json.Unmarshal(credsJson, &UserData)
if err != nil {
return fmt.Errorf("error unmarshaling creds file: %v", err)
}
globals.ApiKey = UserData.ApiKey
globals.AccountId = UserData.AccountId
globals.BaseURL = UserData.BaseURL
globals.UserId = UserData.UserId
return nil
}