/
profile.go
88 lines (76 loc) · 2.02 KB
/
profile.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
package profile
import (
"errors"
"io/ioutil"
"path/filepath"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/config"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
)
// Profile contains the command line options
type Profile struct {
*opts.CommonOptions
}
var (
profileLong = templates.LongDesc(`
Sets the profile for the jx install
`)
profileExample = templates.Examples(`
# Sets the profile for the jx install to cloudbees
jx profile cloudbees
# Set the profile for the jx install to open source
jx profile oss
`)
)
// NewCmdProfile creates the command object
func NewCmdProfile(commonOpts *opts.CommonOptions) *cobra.Command {
options := &Profile{
commonOpts,
}
cmd := &cobra.Command{
Use: "profile <type>",
Short: "Set your jx profile",
Long: profileLong,
Example: profileExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
return cmd
}
// Run implements this command
func (o *Profile) Run() error {
if len(o.Args) < 1 {
return errors.New("Please specify a valid profile of cloudbees or oss ")
}
activatedProfle := config.OpenSourceProfile
if o.Args[0] == config.CloudBeesProfile {
activatedProfle = config.CloudBeesProfile
}
jxHome, err := util.ConfigDir()
if err != nil {
return err
}
profileSettingsFile := filepath.Join(jxHome, config.DefaultProfileFile)
jxProfle := config.JxInstallProfile{
InstallType: activatedProfle,
}
data, err := yaml.Marshal(jxProfle)
if err == nil {
err = ioutil.WriteFile(profileSettingsFile, data, util.DefaultWritePermissions)
if activatedProfle == config.CloudBeesProfile {
log.Logger().Info("Activating the CloudBees Jenkins X Distribution")
} else {
log.Logger().Info("Activating the Jenkins X Profile")
}
}
return err
}