-
Notifications
You must be signed in to change notification settings - Fork 58
/
file.go
110 lines (87 loc) · 2.54 KB
/
file.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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package apmintegration
import (
"os"
"path/filepath"
"strings"
"github.com/ava-labs/apm/types"
"github.com/ava-labs/avalanche-cli/pkg/application"
"gopkg.in/yaml.v3"
)
const yamlExt = ".yaml"
func GetRepos(app *application.Avalanche) ([]string, error) {
repositoryDir := filepath.Join(app.ApmDir, "repositories")
orgs, err := os.ReadDir(repositoryDir)
if err != nil {
return []string{}, err
}
output := []string{}
for _, org := range orgs {
repoDir := filepath.Join(repositoryDir, org.Name())
repos, err := os.ReadDir(repoDir)
if err != nil {
return []string{}, err
}
for _, repo := range repos {
output = append(output, org.Name()+"/"+repo.Name())
}
}
return output, nil
}
func GetSubnets(app *application.Avalanche, repoAlias string) ([]string, error) {
subnetDir := filepath.Join(app.ApmDir, "repositories", repoAlias, "subnets")
subnets, err := os.ReadDir(subnetDir)
if err != nil {
return []string{}, err
}
subnetOptions := make([]string, len(subnets))
for i, subnet := range subnets {
// Remove the .yaml extension
subnetOptions[i] = strings.TrimSuffix(subnet.Name(), filepath.Ext(subnet.Name()))
}
return subnetOptions, nil
}
type SubnetWrapper struct {
Subnet types.Subnet `yaml:"subnet"`
}
type VMWrapper struct {
VM types.VM `yaml:"vm"`
}
func LoadSubnetFile(app *application.Avalanche, subnetKey string) (types.Subnet, error) {
repoAlias, subnetName, err := splitKey(subnetKey)
if err != nil {
return types.Subnet{}, err
}
subnetYamlPath := filepath.Join(app.ApmDir, "repositories", repoAlias, "subnets", subnetName+yamlExt)
var subnetWrapper SubnetWrapper
subnetYamlBytes, err := os.ReadFile(subnetYamlPath)
if err != nil {
return types.Subnet{}, err
}
err = yaml.Unmarshal(subnetYamlBytes, &subnetWrapper)
if err != nil {
return types.Subnet{}, err
}
return subnetWrapper.Subnet, nil
}
func getVMsInSubnet(app *application.Avalanche, subnetKey string) ([]string, error) {
subnet, err := LoadSubnetFile(app, subnetKey)
if err != nil {
return []string{}, err
}
return subnet.VMs, nil
}
func LoadVMFile(app *application.Avalanche, repo, vm string) (types.VM, error) {
vmYamlPath := filepath.Join(app.ApmDir, "repositories", repo, "vms", vm+yamlExt)
var vmWrapper VMWrapper
vmYamlBytes, err := os.ReadFile(vmYamlPath)
if err != nil {
return types.VM{}, err
}
err = yaml.Unmarshal(vmYamlBytes, &vmWrapper)
if err != nil {
return types.VM{}, err
}
return vmWrapper.VM, nil
}