-
Notifications
You must be signed in to change notification settings - Fork 105
/
pko.go
101 lines (86 loc) · 3.15 KB
/
pko.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
package pko
import (
"fmt"
"github.com/openshift/osdctl/cmd/promote/git"
"github.com/openshift/osdctl/cmd/promote/saas"
"github.com/spf13/cobra"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)
func NewCmdPKO() *cobra.Command {
ops := &pkoOptions{}
pkoCmd := &cobra.Command{
Use: "package",
Short: "Utilities to promote package-operator services",
Args: cobra.NoArgs,
DisableAutoGenTag: true,
Example: `
# Promote a package-operator service
osdctl promote package --serviceName <serviceName> --gitHash <git-hash>`,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(ops.ValidatePKOOptions())
appInterface := git.BootstrapOsdCtlForAppInterfaceAndServicePromotions(ops.appInterfaceCheckoutDir)
cmdutil.CheckErr(PromotePackage(appInterface, ops.serviceName, ops.packageTag, ops.hcp))
},
}
pkoCmd.Flags().StringVarP(&ops.serviceName, "serviceName", "n", "", "Service getting promoted")
pkoCmd.Flags().StringVarP(&ops.packageTag, "tag", "t", "", "Package tag being promoted to")
pkoCmd.Flags().StringVarP(&ops.appInterfaceCheckoutDir, "appInterfaceDir", "", "", "location of app-interfache checkout. Falls back to `pwd` and "+git.DefaultAppInterfaceDirectory())
pkoCmd.Flags().BoolVar(&ops.hcp, "hcp", false, "The service being promoted conforms to the HyperShift progressive delivery definition")
return pkoCmd
}
// pkoOptions defines the options provided by this command
type pkoOptions struct {
serviceName string
packageTag string
appInterfaceCheckoutDir string
hcp bool
}
func (p pkoOptions) ValidatePKOOptions() error {
if p.serviceName == "" {
return fmt.Errorf("the service name must be specified with --serviceName/-s")
}
if p.packageTag == "" {
return fmt.Errorf("a new package tag must be provided with '--tag' or '-t'")
}
return nil
}
func PromotePackage(appInterface git.AppInterface, serviceName string, packageTag string, hcp bool) error {
services, err := saas.GetServiceNames(appInterface, saas.OSDSaasDir, saas.BPSaasDir, saas.CADSaasDir)
if err != nil {
return err
}
serviceName, err = saas.ValidateServiceName(services, serviceName)
if err != nil {
return err
}
saasFile, err := saas.GetSaasDir(serviceName, !hcp, hcp)
if err != nil {
return err
}
currentTag, err := git.GetCurrentPackageTagFromAppInterface(saasFile)
if err != nil {
return err
}
if currentTag == packageTag {
return fmt.Errorf("current hash is already at '%s'. Nothing to do", packageTag)
}
branchName := fmt.Sprintf("promote-%s-package-%s", serviceName, packageTag)
err = appInterface.UpdatePackageTag(saasFile, currentTag, packageTag, branchName)
if err != nil {
return err
}
commitMessage := fmt.Sprintf("Promote %s package to %s", serviceName, packageTag)
err = appInterface.CommitSaasFile(saasFile, commitMessage)
if err != nil {
return err
}
fmt.Printf("The current branch (%s) is ready to be pushed\n", branchName)
fmt.Println("")
fmt.Printf("Service: %s\n", serviceName)
fmt.Printf("Previous Tag: %s\n", currentTag)
fmt.Printf("New Tag: %s\n", packageTag)
return nil
}
func updatePackageHash(gitHash, saasFile string) error {
return nil
}