-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
snapshot.go
132 lines (116 loc) · 4.68 KB
/
snapshot.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
package cmd
import (
"fmt"
"github.com/engswee/flashpipe/internal/analytics"
"github.com/engswee/flashpipe/internal/api"
"github.com/engswee/flashpipe/internal/config"
"github.com/engswee/flashpipe/internal/repo"
"github.com/engswee/flashpipe/internal/sync"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"time"
)
func NewSnapshotCommand() *cobra.Command {
snapshotCmd := &cobra.Command{
Use: "snapshot",
Short: "Snapshot integration packages from tenant to Git",
Long: `Snapshot all editable integration packages from SAP Integration Suite
tenant to a Git repository.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
// Validate Draft Handling
draftHandling := config.GetString(cmd, "draft-handling")
switch draftHandling {
case "SKIP", "ADD", "ERROR":
default:
return fmt.Errorf("invalid value for --draft-handling = %v", draftHandling)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
startTime := time.Now()
if err = runSnapshot(cmd); err != nil {
cmd.SilenceUsage = true
}
analytics.Log(cmd, err, startTime)
return
},
}
// Define cobra flags, the default value has the lowest (least significant) precedence
snapshotCmd.Flags().String("dir-git-repo", "", "Directory of Git repository containing contents of artifacts (grouped into packages)")
snapshotCmd.Flags().String("dir-work", "/tmp", "Working directory for in-transit files")
snapshotCmd.Flags().String("draft-handling", "SKIP", "Handling when artifact is in draft version. Allowed values: SKIP, ADD, ERROR")
snapshotCmd.Flags().String("git-commit-msg", "Tenant snapshot of "+time.Now().Format(time.UnixDate), "Message used in commit")
snapshotCmd.Flags().String("git-commit-user", "github-actions[bot]", "User used in commit")
snapshotCmd.Flags().String("git-commit-email", "41898282+github-actions[bot]@users.noreply.github.com", "Email used in commit")
snapshotCmd.Flags().Bool("git-skip-commit", false, "Skip committing changes to Git repository")
snapshotCmd.Flags().Bool("sync-package-details", false, "Sync details of Integration Packages")
_ = snapshotCmd.MarkFlagRequired("dir-git-repo")
return snapshotCmd
}
func runSnapshot(cmd *cobra.Command) error {
log.Info().Msg("Executing snapshot command")
gitRepoDir := config.GetString(cmd, "dir-git-repo")
workDir := config.GetString(cmd, "dir-work")
draftHandling := config.GetString(cmd, "draft-handling")
commitMsg := config.GetString(cmd, "git-commit-msg")
commitUser := config.GetString(cmd, "git-commit-user")
commitEmail := config.GetString(cmd, "git-commit-email")
skipCommit := config.GetBool(cmd, "git-skip-commit")
syncPackageLevelDetails := config.GetBool(cmd, "sync-package-details")
serviceDetails := api.GetServiceDetails(cmd)
err := getTenantSnapshot(serviceDetails, gitRepoDir, workDir, draftHandling, syncPackageLevelDetails)
if err != nil {
return err
}
if !skipCommit {
err = repo.CommitToRepo(gitRepoDir, commitMsg, commitUser, commitEmail)
if err != nil {
return err
}
}
return nil
}
func getTenantSnapshot(serviceDetails *api.ServiceDetails, gitRepoDir string, workDir string, draftHandling string, syncPackageLevelDetails bool) error {
log.Info().Msg("---------------------------------------------------------------------------------")
log.Info().Msg("📢 Begin taking a snapshot of the tenant")
// Initialise HTTP executer
exe := api.InitHTTPExecuter(serviceDetails)
// Get packages from the tenant
ip := api.NewIntegrationPackage(exe)
ids, err := ip.GetPackagesList()
if err != nil {
return err
}
if len(ids) == 0 {
return fmt.Errorf("No packages found in the tenant")
}
log.Info().Msgf("Processing %d packages", len(ids))
synchroniser := sync.New(exe)
for i, id := range ids {
log.Info().Msg("---------------------------------------------------------------------------------")
log.Info().Msgf("Processing package %d/%d - ID: %v", i+1, len(ids), id)
packageWorkingDir := fmt.Sprintf("%v/%v", workDir, id)
packageArtifactsDir := fmt.Sprintf("%v/%v", gitRepoDir, id)
packageDataFromTenant, readOnly, _, err := synchroniser.VerifyDownloadablePackage(id)
if err != nil {
if err != nil {
return err
}
}
if !readOnly {
if syncPackageLevelDetails {
err = synchroniser.PackageToGit(packageDataFromTenant, id, packageWorkingDir, packageArtifactsDir)
if err != nil {
return err
}
}
err = synchroniser.ArtifactsToGit(id, packageWorkingDir, packageArtifactsDir, nil, nil, draftHandling, "ID", nil)
if err != nil {
return err
}
}
}
log.Info().Msg("---------------------------------------------------------------------------------")
log.Info().Msg("🏆 Completed taking a snapshot of the tenant")
return nil
}