|
14 | 14 | package command |
15 | 15 |
|
16 | 16 | import ( |
| 17 | + "context" |
17 | 18 | "fmt" |
18 | 19 | "io/ioutil" |
19 | 20 | "os" |
20 | | - "os/exec" |
21 | 21 | "path/filepath" |
22 | 22 | "strings" |
23 | 23 |
|
24 | 24 | "golang.org/x/mod/modfile" |
| 25 | + |
| 26 | + "github.com/aws-controllers-k8s/code-generator/pkg/util" |
25 | 27 | ) |
26 | 28 |
|
27 | 29 | const ( |
@@ -64,67 +66,92 @@ func isDirWriteable(fp string) bool { |
64 | 66 | // ensureSDKRepo ensures that we have a git clone'd copy of the aws-sdk-go |
65 | 67 | // repository, which we use model JSON files from. Upon successful return of |
66 | 68 | // this function, the sdkDir global variable will be set to the directory where |
67 | | -// the aws-sdk-go is found |
68 | | -func ensureSDKRepo(cacheDir string) error { |
| 69 | +// the aws-sdk-go is found. It will also optionally fetch all the remote tags |
| 70 | +// and checkout the given tag. |
| 71 | +func ensureSDKRepo( |
| 72 | + cacheDir string, |
| 73 | + // A boolean instructing ensureSDKRepo whether to fetch the remote tags from |
| 74 | + // the upstream repository |
| 75 | + fetchTags bool, |
| 76 | +) error { |
69 | 77 | var err error |
70 | 78 | srcPath := filepath.Join(cacheDir, "src") |
71 | 79 | if err = os.MkdirAll(srcPath, os.ModePerm); err != nil { |
72 | 80 | return err |
73 | 81 | } |
74 | | - // clone the aws-sdk-go repository locally so we can query for API |
75 | | - // information in the models/apis/ directories |
76 | | - sdkDir, err = cloneSDKRepo(srcPath) |
77 | | - return err |
78 | | -} |
79 | 82 |
|
80 | | -// cloneSDKRepo git clone's the aws-sdk-go source repo into the cache and |
81 | | -// returns the filepath to the clone'd repo. If the aws-sdk-go repository |
82 | | -// already exists in the cache, it will checkout the current sdk-go version |
83 | | -// mentionned in 'go.mod' file. |
84 | | -func cloneSDKRepo(srcPath string) (string, error) { |
85 | | - sdkVersion, err := getSDKVersion() |
86 | | - if err != nil { |
87 | | - return "", err |
| 83 | + // Clone repository if it doen't exist |
| 84 | + sdkDir = filepath.Join(srcPath, "aws-sdk-go") |
| 85 | + if _, err := os.Stat(sdkDir); os.IsNotExist(err) { |
| 86 | + err = util.CloneRepository(context.Background(), sdkDir, sdkRepoURL) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("canot clone repository: %v", err) |
| 89 | + } |
88 | 90 | } |
89 | | - clonePath := filepath.Join(srcPath, "aws-sdk-go") |
90 | | - if optRefreshCache { |
91 | | - if _, err := os.Stat(filepath.Join(clonePath, ".git")); !os.IsNotExist(err) { |
92 | | - cmd := exec.Command("git", "-C", clonePath, "fetch", "--all", "--tags") |
93 | | - if err = cmd.Run(); err != nil { |
94 | | - return "", err |
95 | | - } |
96 | | - cmd = exec.Command("git", "-C", clonePath, "checkout", "tags/"+sdkVersion) |
97 | | - return clonePath, cmd.Run() |
| 91 | + |
| 92 | + // Fetch all tags |
| 93 | + if fetchTags { |
| 94 | + err = util.FetchRepositoryTags(context.Background(), sdkDir) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("cannot fetch tags: %v", err) |
98 | 97 | } |
99 | 98 | } |
100 | | - if _, err := os.Stat(clonePath); os.IsNotExist(err) { |
101 | | - cmd := exec.Command("git", "clone", "-b", sdkVersion, sdkRepoURL, clonePath) |
102 | | - return clonePath, cmd.Run() |
| 99 | + |
| 100 | + // get sdkVersion and ensure it prefix |
| 101 | + // TODO(a-hilaly) Parse `ack-generate-metadata.yaml` and pass the aws-sdk-go |
| 102 | + // version here. |
| 103 | + sdkVersion, err := getSDKVersion("") |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + sdkVersion = ensureSemverPrefix(sdkVersion) |
| 108 | + |
| 109 | + repo, err := util.LoadRepository(sdkDir) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("cannot read local repository: %v", err) |
103 | 112 | } |
104 | | - return clonePath, nil |
| 113 | + |
| 114 | + // Now checkout the local repository. |
| 115 | + err = util.CheckoutRepositoryTag(repo, sdkVersion) |
| 116 | + if err != nil { |
| 117 | + return fmt.Errorf("cannot checkout tag: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | + return err |
| 121 | +} |
| 122 | + |
| 123 | +// ensureSemverPrefix takes a semver string and tries to append the 'v' |
| 124 | +// prefix if it's missing. |
| 125 | +func ensureSemverPrefix(s string) string { |
| 126 | + // trim all leading 'v' runes (characters) |
| 127 | + s = strings.TrimLeft(s, "v") |
| 128 | + return fmt.Sprintf("v%s", s) |
105 | 129 | } |
106 | 130 |
|
107 | 131 | // getSDKVersion returns the github.com/aws/aws-sdk-go version to use. It |
108 | | -// first tries to get return version from the --aws-sdk-go-version flag, then |
109 | | -// look for the service controller and local go.mod files. |
110 | | -func getSDKVersion() (string, error) { |
| 132 | +// first tries to get the version from the --aws-sdk-go-version flag, then |
| 133 | +// from the ack-generate-metadata.yaml and finally look for the service |
| 134 | +// go.mod controller. |
| 135 | +func getSDKVersion( |
| 136 | + lastGenerationVersion string, |
| 137 | +) (string, error) { |
111 | 138 | // First try to get the version from --aws-sdk-go-version flag |
112 | 139 | if optAWSSDKGoVersion != "" { |
113 | 140 | return optAWSSDKGoVersion, nil |
114 | 141 | } |
115 | 142 |
|
| 143 | + // then, try to use last generation version (from ack-generate-metadata.yaml) |
| 144 | + if lastGenerationVersion != "" { |
| 145 | + return lastGenerationVersion, nil |
| 146 | + } |
| 147 | + |
116 | 148 | // then, try to parse the service controller go.mod file |
117 | 149 | sdkVersion, err := getSDKVersionFromGoMod(filepath.Join(optOutputPath, "go.mod")) |
118 | 150 | if err == nil { |
119 | 151 | return sdkVersion, nil |
120 | 152 | } |
121 | 153 |
|
122 | | - // then try to parse a local go.mod |
123 | | - sdkVersion, err = getSDKVersionFromGoMod("go.mod") |
124 | | - if err != nil { |
125 | | - return "", err |
126 | | - } |
127 | | - return sdkVersion, nil |
| 154 | + return "", err |
128 | 155 | } |
129 | 156 |
|
130 | 157 | // getSDKVersionFromGoMod parses a given go.mod file and returns |
|
0 commit comments