1
1
package main
2
2
3
3
import (
4
+ "errors"
4
5
"flag"
5
6
"fmt"
6
7
"github.com/mcasperson/OctopusTerraformExport/internal/client"
7
8
"github.com/mcasperson/OctopusTerraformExport/internal/converters"
9
+ "github.com/mcasperson/OctopusTerraformExport/internal/model/octopus"
8
10
"github.com/mcasperson/OctopusTerraformExport/internal/strutil"
9
11
"github.com/mcasperson/OctopusTerraformExport/internal/writers"
10
12
"os"
11
13
"strings"
12
14
)
13
15
14
16
func main () {
15
- url , space , apiKey , dest , console , projectId := parseUrl ()
17
+ url , space , apiKey , dest , console , projectId , projectName := parseUrl ()
16
18
17
19
var err error = nil
18
20
19
- if projectId != "" {
21
+ if projectName != "" {
22
+ projectId , err := convertProjectNameToId (url , space , apiKey , projectName )
23
+
24
+ if err != nil {
25
+ fmt .Println (err .Error ())
26
+ os .Exit (1 )
27
+ }
28
+
29
+ err = ConvertProjectToTerraform (url , space , apiKey , dest , console , projectId )
30
+ } else if projectId != "" {
20
31
err = ConvertProjectToTerraform (url , space , apiKey , dest , console , projectId )
21
32
} else {
22
33
err = ConvertSpaceToTerraform (url , space , apiKey , dest , console )
@@ -28,6 +39,25 @@ func main() {
28
39
}
29
40
}
30
41
42
+ func convertProjectNameToId (url string , space string , apiKey string , name string ) (string , error ) {
43
+ client := client.OctopusClient {
44
+ Url : url ,
45
+ Space : space ,
46
+ ApiKey : apiKey ,
47
+ }
48
+
49
+ collection := octopus.GeneralCollection [octopus.Project ]{}
50
+ client .GetAllResources ("Projects" , & collection , []string {"name" , name })
51
+
52
+ for _ , p := range collection .Items {
53
+ if p .Name == name {
54
+ return p .Id , nil
55
+ }
56
+ }
57
+
58
+ return "" , errors .New ("did not find project with name " + name )
59
+ }
60
+
31
61
func ConvertSpaceToTerraform (url string , space string , apiKey string , dest string , console bool ) error {
32
62
client := client.OctopusClient {
33
63
Url : url ,
@@ -242,7 +272,7 @@ func processResources(resources []converters.ResourceDetails) (map[string]string
242
272
return fileMap , nil
243
273
}
244
274
245
- func parseUrl () (string , string , string , string , bool , string ) {
275
+ func parseUrl () (string , string , string , string , bool , string , string ) {
246
276
var url string
247
277
flag .StringVar (& url , "url" , "" , "The Octopus URL e.g. https://myinstance.octopus.app" )
248
278
@@ -261,9 +291,12 @@ func parseUrl() (string, string, string, string, bool, string) {
261
291
var projectId string
262
292
flag .StringVar (& projectId , "projectId" , "" , "Limit the export to a single project" )
263
293
294
+ var projectName string
295
+ flag .StringVar (& projectName , "projectName" , "" , "Limit the export to a single project" )
296
+
264
297
flag .Parse ()
265
298
266
- return url , space , apiKey , dest , console , projectId
299
+ return url , space , apiKey , dest , console , projectId , projectName
267
300
}
268
301
269
302
func writeFiles (files map [string ]string , dest string , console bool ) error {
0 commit comments