Skip to content

Commit 3b47dbd

Browse files
Merge pull request #504 from NautiluX/appinterface-dir
promote: Allow providing app-interface checkout directory
2 parents b17e73a + 13bcf91 commit 3b47dbd

File tree

5 files changed

+102
-47
lines changed

5 files changed

+102
-47
lines changed

cmd/promote/git/app_interface.go

+69-19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"os"
77
"os/exec"
8+
"path/filepath"
89
"strings"
910

1011
"gopkg.in/yaml.v3"
@@ -23,21 +24,49 @@ type Service struct {
2324
} `yaml:"resourceTemplates"`
2425
}
2526

26-
func BootstrapOsdCtlForAppInterfaceAndServicePromotions() {
27-
_, err := getBaseDir()
28-
if err != nil {
29-
log.Fatal(err)
27+
type AppInterface struct {
28+
GitDirectory string
29+
}
30+
31+
func DefaultAppInterfaceDirectory() string {
32+
return filepath.Join(os.Getenv("HOME"), "git", "app-interface")
33+
}
34+
35+
func BootstrapOsdCtlForAppInterfaceAndServicePromotions(appInterfaceCheckoutDir string) AppInterface {
36+
a := AppInterface{}
37+
if appInterfaceCheckoutDir != "" {
38+
a.GitDirectory = appInterfaceCheckoutDir
39+
err := checkAppInterfaceCheckout(a.GitDirectory)
40+
if err != nil {
41+
log.Fatalf("Provided directory %s is not an AppInterface directory: %v", a.GitDirectory, err)
42+
}
43+
return a
3044
}
31-
err = checkAppInterfaceCheckout()
45+
46+
dir, err := getBaseDir()
47+
if err == nil {
48+
a.GitDirectory = dir
49+
err = checkAppInterfaceCheckout(a.GitDirectory)
50+
if err == nil {
51+
return a
52+
}
53+
}
54+
55+
log.Printf("Not running in AppInterface directory: %v - Trying %s next\n", err, DefaultAppInterfaceDirectory())
56+
a.GitDirectory = DefaultAppInterfaceDirectory()
57+
err = checkAppInterfaceCheckout(a.GitDirectory)
3258
if err != nil {
33-
log.Fatal(err)
59+
log.Fatalf("%s is not an AppInterface directory: %v", DefaultAppInterfaceDirectory(), err)
3460
}
61+
62+
log.Printf("Found AppInterface in %s.\n", a.GitDirectory)
63+
return a
3564
}
3665

3766
// checkAppInterfaceCheckout checks if the script is running in the checkout of app-interface
38-
func checkAppInterfaceCheckout() error {
67+
func checkAppInterfaceCheckout(directory string) error {
3968
cmd := exec.Command("git", "remote", "-v")
40-
cmd.Dir = BaseDir
69+
cmd.Dir = directory
4170
output, err := cmd.CombinedOutput()
4271
if err != nil {
4372
return fmt.Errorf("error executing 'git remote -v': %v", err)
@@ -49,7 +78,7 @@ func checkAppInterfaceCheckout() error {
4978
if !strings.Contains(outputString, "gitlab.cee.redhat.com") && !strings.Contains(outputString, "app-interface") {
5079
return fmt.Errorf("not running in checkout of app-interface")
5180
}
52-
fmt.Println("Running in checkout of app-interface.")
81+
//fmt.Println("Running in checkout of app-interface.")
5382

5483
return nil
5584
}
@@ -149,10 +178,24 @@ func GetCurrentPackageTagFromAppInterface(saasFile string) (string, error) {
149178
return currentPackageTag, nil
150179
}
151180

152-
func UpdateAppInterface(serviceName, saasFile, currentGitHash, promotionGitHash, branchName string) error {
153-
cmd := exec.Command("git", "checkout", "-b", branchName, "master")
154-
cmd.Dir = BaseDir
181+
func (a AppInterface) UpdateAppInterface(serviceName, saasFile, currentGitHash, promotionGitHash, branchName string) error {
182+
cmd := exec.Command("git", "checkout", "master")
183+
cmd.Dir = a.GitDirectory
155184
err := cmd.Run()
185+
if err != nil {
186+
return fmt.Errorf("failed to checkout master branch: %v", err)
187+
}
188+
189+
cmd = exec.Command("git", "branch", "-D", branchName)
190+
cmd.Dir = a.GitDirectory
191+
err = cmd.Run()
192+
if err != nil {
193+
fmt.Printf("failed to cleanup branch %s: %v, continuing to create it.\n", branchName, err)
194+
}
195+
196+
cmd = exec.Command("git", "checkout", "-b", branchName, "master")
197+
cmd.Dir = a.GitDirectory
198+
err = cmd.Run()
156199
if err != nil {
157200
return fmt.Errorf("failed to create branch %s: %v, does it already exist? If so, please delete it with `git branch -D %s` first", branchName, err, branchName)
158201
}
@@ -174,12 +217,19 @@ func UpdateAppInterface(serviceName, saasFile, currentGitHash, promotionGitHash,
174217
return nil
175218
}
176219

177-
func UpdatePackageTag(saasFile, oldTag, promotionTag, branchName string) error {
178-
cmd := exec.Command("git", "checkout", "-b", branchName, "master")
179-
cmd.Dir = BaseDir
220+
func (a AppInterface) UpdatePackageTag(saasFile, oldTag, promotionTag, branchName string) error {
221+
cmd := exec.Command("git", "checkout", "master")
222+
cmd.Dir = a.GitDirectory
180223
err := cmd.Run()
181224
if err != nil {
182-
return fmt.Errorf("failed to create branch %s: %v, does it already exist? If so, please delete it with `git branch -D %s` first", branchName, err, branchName)
225+
return fmt.Errorf("failed to checkout master branch: %v", err)
226+
}
227+
228+
cmd = exec.Command("git", "branch", "-D", branchName)
229+
cmd.Dir = a.GitDirectory
230+
err = cmd.Run()
231+
if err != nil {
232+
fmt.Printf("failed to cleanup branch %s: %v, continuing to create it.\n", branchName, err)
183233
}
184234

185235
// Update the hash in the SAAS file
@@ -198,18 +248,18 @@ func UpdatePackageTag(saasFile, oldTag, promotionTag, branchName string) error {
198248
return nil
199249
}
200250

201-
func CommitSaasFile(saasFile, commitMessage string) error {
251+
func (a AppInterface) CommitSaasFile(saasFile, commitMessage string) error {
202252
// Commit the change
203253
cmd := exec.Command("git", "add", saasFile)
204-
cmd.Dir = BaseDir
254+
cmd.Dir = a.GitDirectory
205255
err := cmd.Run()
206256
if err != nil {
207257
return fmt.Errorf("failed to add file %s: %v", saasFile, err)
208258
}
209259

210260
//commitMessage := fmt.Sprintf("Promote %s to %s", serviceName, promotionGitHash)
211261
cmd = exec.Command("git", "commit", "-m", commitMessage)
212-
cmd.Dir = BaseDir
262+
cmd.Dir = a.GitDirectory
213263
err = cmd.Run()
214264
if err != nil {
215265
return fmt.Errorf("failed to commit changes: %v", err)

cmd/promote/git/git_cmd.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ func getBaseDir() (string, error) {
2929
return BaseDir, baseDirErr
3030
}
3131

32-
func checkBehindMaster() error {
32+
func checkBehindMaster(dir string) error {
3333
fmt.Printf("### Checking 'master' branch is up to date ###\n")
3434

3535
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
36-
cmd.Dir = BaseDir
36+
cmd.Dir = dir
3737
output, err := cmd.Output()
3838
if err != nil {
3939
return fmt.Errorf("error executing git rev-parse command: %v", err)
@@ -46,14 +46,14 @@ func checkBehindMaster() error {
4646

4747
// Fetch the latest changes from the upstream repository
4848
cmd = exec.Command("git", "fetch", "upstream")
49-
cmd.Dir = BaseDir
49+
cmd.Dir = dir
5050
err = cmd.Run()
5151
if err != nil {
5252
return fmt.Errorf("error executing git fetch command: %v", err)
5353
}
5454

5555
cmd = exec.Command("git", "rev-list", "--count", "HEAD..upstream/master")
56-
cmd.Dir = BaseDir
56+
cmd.Dir = dir
5757
output, err = cmd.Output()
5858
if err != nil {
5959
return fmt.Errorf("error executing git rev-list command: %v", err)

cmd/promote/pko/pko.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,24 @@ func NewCmdPKO() *cobra.Command {
2121
osdctl promote package --serviceName <serviceName> --gitHash <git-hash>`,
2222
Run: func(cmd *cobra.Command, args []string) {
2323
cmdutil.CheckErr(ops.ValidatePKOOptions())
24-
git.BootstrapOsdCtlForAppInterfaceAndServicePromotions()
24+
appInterface := git.BootstrapOsdCtlForAppInterfaceAndServicePromotions(ops.appInterfaceCheckoutDir)
2525

26-
cmdutil.CheckErr(PromotePackage(ops.serviceName, ops.packageTag, ops.hcp))
26+
cmdutil.CheckErr(PromotePackage(appInterface, ops.serviceName, ops.packageTag, ops.hcp))
2727
},
2828
}
2929
pkoCmd.Flags().StringVarP(&ops.serviceName, "serviceName", "n", "", "Service getting promoted")
3030
pkoCmd.Flags().StringVarP(&ops.packageTag, "tag", "t", "", "Package tag being promoted to")
31+
pkoCmd.Flags().StringVarP(&ops.appInterfaceCheckoutDir, "appInterfaceDir", "", "", "location of app-interfache checkout. Falls back to `pwd` and "+git.DefaultAppInterfaceDirectory())
3132
pkoCmd.Flags().BoolVar(&ops.hcp, "hcp", false, "The service being promoted conforms to the HyperShift progressive delivery definition")
3233
return pkoCmd
3334
}
3435

3536
// pkoOptions defines the options provided by this command
3637
type pkoOptions struct {
37-
serviceName string
38-
packageTag string
39-
hcp bool
38+
serviceName string
39+
packageTag string
40+
appInterfaceCheckoutDir string
41+
hcp bool
4042
}
4143

4244
func (p pkoOptions) ValidatePKOOptions() error {
@@ -49,8 +51,8 @@ func (p pkoOptions) ValidatePKOOptions() error {
4951
return nil
5052
}
5153

52-
func PromotePackage(serviceName string, packageTag string, hcp bool) error {
53-
services, err := saas.GetServiceNames(saas.OSDSaasDir, saas.BPSaasDir, saas.CADSaasDir)
54+
func PromotePackage(appInterface git.AppInterface, serviceName string, packageTag string, hcp bool) error {
55+
services, err := saas.GetServiceNames(appInterface, saas.OSDSaasDir, saas.BPSaasDir, saas.CADSaasDir)
5456
if err != nil {
5557
return err
5658
}
@@ -75,13 +77,13 @@ func PromotePackage(serviceName string, packageTag string, hcp bool) error {
7577
}
7678

7779
branchName := fmt.Sprintf("promote-%s-package-%s", serviceName, packageTag)
78-
err = git.UpdatePackageTag(saasFile, currentTag, packageTag, branchName)
80+
err = appInterface.UpdatePackageTag(saasFile, currentTag, packageTag, branchName)
7981
if err != nil {
8082
return err
8183
}
8284

8385
commitMessage := fmt.Sprintf("Promote %s package to %s", serviceName, packageTag)
84-
err = git.CommitSaasFile(saasFile, commitMessage)
86+
err = appInterface.CommitSaasFile(saasFile, commitMessage)
8587
if err != nil {
8688
return err
8789
}

cmd/promote/saas/saas.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ type saasOptions struct {
1313
osd bool
1414
hcp bool
1515

16-
serviceName string
17-
gitHash string
16+
appInterfaceCheckoutDir string
17+
serviceName string
18+
gitHash string
1819
}
1920

2021
// newCmdSaas implementes the saas command to interact with promoting SaaS services/operators
@@ -35,15 +36,15 @@ func NewCmdSaas() *cobra.Command {
3536
osdctl promote saas --serviceName <service-name> --gitHash <git-hash> --hcp`,
3637
Run: func(cmd *cobra.Command, args []string) {
3738
ops.validateSaasFlow()
38-
git.BootstrapOsdCtlForAppInterfaceAndServicePromotions()
39+
appInterface := git.BootstrapOsdCtlForAppInterfaceAndServicePromotions(ops.appInterfaceCheckoutDir)
3940

4041
if ops.list {
4142
if ops.serviceName != "" || ops.gitHash != "" || ops.osd || ops.hcp {
4243
fmt.Printf("Error: --list cannot be used with any other flags\n\n")
4344
cmd.Help()
4445
os.Exit(1)
4546
}
46-
listServiceNames()
47+
listServiceNames(appInterface)
4748
os.Exit(0)
4849
}
4950

@@ -53,7 +54,7 @@ func NewCmdSaas() *cobra.Command {
5354
os.Exit(1)
5455
}
5556

56-
err := servicePromotion(ops.serviceName, ops.gitHash, ops.osd, ops.hcp)
57+
err := servicePromotion(appInterface, ops.serviceName, ops.gitHash, ops.osd, ops.hcp)
5758
if err != nil {
5859
fmt.Printf("Error while promoting service: %v\n", err)
5960
os.Exit(1)
@@ -69,13 +70,14 @@ func NewCmdSaas() *cobra.Command {
6970
saasCmd.Flags().StringVarP(&ops.gitHash, "gitHash", "g", "", "Git hash of the SaaS service/operator commit getting promoted")
7071
saasCmd.Flags().BoolVarP(&ops.osd, "osd", "", false, "OSD service/operator getting promoted")
7172
saasCmd.Flags().BoolVarP(&ops.hcp, "hcp", "", false, "Git hash of the SaaS service/operator commit getting promoted")
73+
saasCmd.Flags().StringVarP(&ops.appInterfaceCheckoutDir, "appInterfaceDir", "", "", "location of app-interfache checkout. Falls back to `pwd` and "+git.DefaultAppInterfaceDirectory())
7274

7375
return saasCmd
7476
}
7577

7678
func (o *saasOptions) validateSaasFlow() {
77-
if o.serviceName == "" || o.gitHash == "" {
78-
fmt.Printf("Usage: For SaaS services/operators, please provide --serviceName and --gitHash\n")
79+
if o.serviceName == "" && o.gitHash == "" {
80+
fmt.Printf("Usage: For SaaS services/operators, please provide --serviceName and (optional) --gitHash\n")
7981
fmt.Printf("--serviceName is the name of the service, i.e. saas-managed-cluster-config\n")
8082
fmt.Printf("--gitHash is the target git commit in the service, if not specified defaults to HEAD of master\n\n")
8183
return

cmd/promote/saas/utils.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ var (
2121
ServicesFilesMap = map[string]string{}
2222
)
2323

24-
func listServiceNames() error {
25-
_, err := GetServiceNames(OSDSaasDir, BPSaasDir, CADSaasDir)
24+
func listServiceNames(appInterface git.AppInterface) error {
25+
_, err := GetServiceNames(appInterface, OSDSaasDir, BPSaasDir, CADSaasDir)
2626
if err != nil {
2727
return err
2828
}
@@ -36,8 +36,8 @@ func listServiceNames() error {
3636
return nil
3737
}
3838

39-
func servicePromotion(serviceName, gitHash string, osd, hcp bool) error {
40-
_, err := GetServiceNames(OSDSaasDir, BPSaasDir, CADSaasDir)
39+
func servicePromotion(appInterface git.AppInterface, serviceName, gitHash string, osd, hcp bool) error {
40+
_, err := GetServiceNames(appInterface, OSDSaasDir, BPSaasDir, CADSaasDir)
4141
if err != nil {
4242
return err
4343
}
@@ -74,13 +74,13 @@ func servicePromotion(serviceName, gitHash string, osd, hcp bool) error {
7474
fmt.Printf("Service: %s will be promoted to %s\n", serviceName, promotionGitHash)
7575

7676
branchName := fmt.Sprintf("promote-%s-%s", serviceName, promotionGitHash)
77-
err = git.UpdateAppInterface(serviceName, saasDir, currentGitHash, promotionGitHash, branchName)
77+
err = appInterface.UpdateAppInterface(serviceName, saasDir, currentGitHash, promotionGitHash, branchName)
7878
if err != nil {
7979
fmt.Printf("FAILURE: %v\n", err)
8080
}
8181

8282
commitMessage := fmt.Sprintf("Promote %s to %s\n\nSee %s/compare/%s...%s for contents of the promotion.", serviceName, promotionGitHash, serviceRepo, currentGitHash, promotionGitHash)
83-
err = git.CommitSaasFile(saasDir, commitMessage)
83+
err = appInterface.CommitSaasFile(saasDir, commitMessage)
8484
if err != nil {
8585
return fmt.Errorf("failed to commit changes to app-interface: %w", err)
8686
}
@@ -94,8 +94,9 @@ func servicePromotion(serviceName, gitHash string, osd, hcp bool) error {
9494
return nil
9595
}
9696

97-
func GetServiceNames(saaDirs ...string) ([]string, error) {
98-
baseDir := git.BaseDir
97+
func GetServiceNames(appInterface git.AppInterface, saaDirs ...string) ([]string, error) {
98+
baseDir := appInterface.GitDirectory
99+
99100
for _, dir := range saaDirs {
100101
dirGlob := filepath.Join(baseDir, dir, "saas-*")
101102
filepaths, err := filepath.Glob(dirGlob)

0 commit comments

Comments
 (0)