/
pro.go
145 lines (129 loc) · 3.78 KB
/
pro.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
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2022 Aspect Build Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pro
import (
"context"
"fmt"
"aspect.build/cli/pkg/aspect/root/config"
"aspect.build/cli/pkg/bazel/workspace"
"aspect.build/cli/pkg/ioutils"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)
type Pro struct {
ioutils.Streams
}
func New(streams ioutils.Streams) *Pro {
return &Pro{
Streams: streams,
}
}
func (runner *Pro) Run(ctx context.Context, _ *cobra.Command, args []string) error {
if err := enableProForUser(); err != nil {
return err
}
if err := enableProForWorkspace(); err != nil {
return err
}
return nil
}
// TODO: move promptui helpers to shared library
type ConfirmationRunner interface {
Run() (string, error)
}
func Confirmation(question string) ConfirmationRunner {
return &promptui.Prompt{
Label: question,
IsConfirm: true,
}
}
func enableProForUser() error {
homeConfig, err := config.LoadHomeConfig()
if err != nil {
return err
}
version, err := config.ParseConfigVersion(homeConfig.GetString("version"))
proTier := config.IsProTier(version.Tier)
if !proTier {
_, err = Confirmation("Enable Aspect CLI Pro features for user").Run()
if err == nil {
// TODO: show Aspect commercial license when it is finalized and/or send user to a "Sign up for free trial period" page
configFile, created, err := config.SetInHomeConfig("version", toProVersion(version.Version))
if err != nil {
return err
}
if created {
fmt.Printf("Created %s\n", configFile)
} else {
fmt.Printf("Updated %s\n", configFile)
}
}
} else {
configFile, _ := config.HomeConfigFile()
if configFile == "" {
fmt.Println("Aspect CLI Pro features already enabled for user")
} else {
fmt.Printf("Aspect CLI Pro features already enabled for user in %s\n", configFile)
}
}
return nil
}
func enableProForWorkspace() error {
workspaceConfig, err := config.LoadWorkspaceConfig()
if err != nil {
// Ignore err if it is a workspace.NotFoundError
if _, ok := err.(*workspace.NotFoundError); !ok {
return err
}
}
if workspaceConfig == nil {
// We're are not currently in a Bazel workspace
fmt.Println("Run 'aspect pro' in a Bazel workspace to enable Aspect CLI Pro features for that workspace")
return nil
}
version, err := config.ParseConfigVersion(workspaceConfig.GetString("version"))
proTier := config.IsProTier(version.Tier)
if !proTier {
_, err = Confirmation("Enable Aspect CLI Pro features for workspace").Run()
if err == nil {
// TODO: show Aspect commercial license when it is finalized and/or send user to a "Sign up for free trial period" page
configFile, created, err := config.SetInWorkspaceConfig("version", toProVersion(version.Version))
if err != nil {
return err
}
if created {
fmt.Printf("Created %s\n", configFile)
} else {
fmt.Printf("Updated %s\n", configFile)
}
}
} else {
configFile, _ := config.WorkspaceConfigFile()
if configFile == "" {
fmt.Println("Aspect CLI Pro features already enabled for workspace")
} else {
fmt.Printf("Aspect CLI Pro features already enabled for workspace in %s\n", configFile)
}
}
return nil
}
func toProVersion(version string) string {
if len(version) == 0 {
return "pro"
} else {
return fmt.Sprintf("pro/%s", version)
}
}