Skip to content

Commit a82e8cc

Browse files
committed
Added ability to export by project name, and fixed variable file names
1 parent c294cf9 commit a82e8cc

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

cmd/octoterra.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
package main
22

33
import (
4+
"errors"
45
"flag"
56
"fmt"
67
"github.com/mcasperson/OctopusTerraformExport/internal/client"
78
"github.com/mcasperson/OctopusTerraformExport/internal/converters"
9+
"github.com/mcasperson/OctopusTerraformExport/internal/model/octopus"
810
"github.com/mcasperson/OctopusTerraformExport/internal/strutil"
911
"github.com/mcasperson/OctopusTerraformExport/internal/writers"
1012
"os"
1113
"strings"
1214
)
1315

1416
func main() {
15-
url, space, apiKey, dest, console, projectId := parseUrl()
17+
url, space, apiKey, dest, console, projectId, projectName := parseUrl()
1618

1719
var err error = nil
1820

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 != "" {
2031
err = ConvertProjectToTerraform(url, space, apiKey, dest, console, projectId)
2132
} else {
2233
err = ConvertSpaceToTerraform(url, space, apiKey, dest, console)
@@ -28,6 +39,25 @@ func main() {
2839
}
2940
}
3041

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+
3161
func ConvertSpaceToTerraform(url string, space string, apiKey string, dest string, console bool) error {
3262
client := client.OctopusClient{
3363
Url: url,
@@ -242,7 +272,7 @@ func processResources(resources []converters.ResourceDetails) (map[string]string
242272
return fileMap, nil
243273
}
244274

245-
func parseUrl() (string, string, string, string, bool, string) {
275+
func parseUrl() (string, string, string, string, bool, string, string) {
246276
var url string
247277
flag.StringVar(&url, "url", "", "The Octopus URL e.g. https://myinstance.octopus.app")
248278

@@ -261,9 +291,12 @@ func parseUrl() (string, string, string, string, bool, string) {
261291
var projectId string
262292
flag.StringVar(&projectId, "projectId", "", "Limit the export to a single project")
263293

294+
var projectName string
295+
flag.StringVar(&projectName, "projectName", "", "Limit the export to a single project")
296+
264297
flag.Parse()
265298

266-
return url, space, apiKey, dest, console, projectId
299+
return url, space, apiKey, dest, console, projectId, projectName
267300
}
268301

269302
func writeFiles(files map[string]string, dest string, console bool) error {

internal/converters/variable_set_converter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (c VariableSetConverter) toHcl(resource octopus.VariableSet, recursive bool
4141
for _, v := range resource.Variables {
4242
thisResource := ResourceDetails{}
4343

44-
resourceName := parentName + "_" + sanitizer.SanitizeName(v.Name)
44+
resourceName := sanitizer.SanitizeName(parentName) + "_" + sanitizer.SanitizeName(v.Name)
4545

4646
if recursive {
4747
// Export linked accounts

0 commit comments

Comments
 (0)