/
check.go
109 lines (96 loc) · 3.35 KB
/
check.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
//nolint:gochecknoglobals // cobra uses globals for commands
package cmd
import (
"errors"
"os"
"os/exec"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
// checkCmd represents the check command
var checkCmd = &cobra.Command{
Use: "check",
Short: "🧪 Check your system for the correct requirements",
Long: `Checks your system for tooling that is required and environment variables.
For example:
- Required environment variable for authenticating with Azure CLI
- Detection of the Azure CLI.
`,
Run: func(cmd *cobra.Command, args []string) {
check()
},
}
func init() {
rootCmd.AddCommand(checkCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// checkCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// checkCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func check() {
if Debug {
pterm.EnableDebugMessages()
}
failureCount := 0
report := pterm.TableData{
[]string{"Status", "Check", "Value", "Notes"},
}
checkReport, err := checkAzEnv()
if err != nil {
failureCount++
}
report = append(report, checkReport)
checkReport, err = checkAzureCLI()
if err != nil {
failureCount++
}
report = append(report, checkReport)
primary := pterm.NewStyle(pterm.FgLightCyan, pterm.BgGray, pterm.Bold)
pterm.DefaultHeader.Printfln("az-pr requirements check")
if err := pterm.DefaultTable.WithHasHeader().
WithBoxed(true).
WithHeaderStyle(primary).
WithData(report).Render(); err != nil {
pterm.Error.WithShowLineNumber(true).WithLineNumberOffset(1).Printfln(
"pterm.DefaultTable.WithHasHeader of variable information failed. Continuing...%v",
err,
)
}
if failureCount > 0 {
pterm.Error.Printfln("failureCount: %d", failureCount)
os.Exit(1)
} else {
pterm.Success.Printfln("no failures: %d", failureCount)
}
}
// checkAzureCLI checks if Azure CLI is installed.
func checkAzureCLI() ([]string, error) {
if Debug {
pterm.EnableDebugMessages()
}
_, err := exec.LookPath("az")
if err != nil {
// pterm.Error.Println("🧪 Azure CLI is not installed.\n" +
// "This is required to be able to create pull requests.\n" +
// "👉 Recommend installing Azure CLI: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli")
return []string{"❌", "azure-cli", "required", "not installed"}, errors.New("Azure CLI not installed")
}
return []string{"✅", "azure-cli", "installed", ""}, nil
}
// checkAzEnv checks if AZURE_DEVOPS_EXT_PAT is set as env variable which is required for the Azure CLI to function.
func checkAzEnv() ([]string, error) {
if Debug {
pterm.EnableDebugMessages()
}
_, ok := os.LookupEnv("AZURE_DEVOPS_EXT_PAT")
if !ok || os.Getenv("AZURE_DEVOPS_EXT_PAT") == "" {
// pterm.Error.Println("🧪 AZURE_DEVOPS_EXT_PAT is not set as env variable.\n" +
// "This is required to be able to create pull requests.\n" +
// "👉 Recommend export AZURE_DEVOPS_EXT_PAT=\"\" in your $HOME/.envrc, .zshenv, .bashprofile, etc.")
return []string{"❌", "AZURE_DEVOPS_EXT_PAT", "not set", "env var is required for auth UNLESS using SSO based login (better security ⚡)"}, errors.New("AZURE_DEVOPS_EXT_PAT not set")
}
return []string{"✅", "AZURE_DEVOPS_EXT_PAT", "detected", ""}, nil
}