11package cmd
22
33import (
4+ "errors"
45 "fmt"
5- "os"
6- "os/user"
6+ "path/filepath"
77 "strings"
88
9- "github.com/dhth/ecsv/ui "
9+ "github.com/dhth/ecsv/internal/types "
1010
1111 "gopkg.in/yaml.v3"
1212)
1313
14- type T struct {
14+ var errInvalidConfigSourceProvided = errors .New ("invalid aws-system-source provided" )
15+
16+ type Config struct {
1517 EnvSequence []string `yaml:"env-sequence"`
1618 Systems []struct {
1719 Key string `yaml:"key"`
@@ -26,54 +28,46 @@ type T struct {
2628 } `yaml:"systems"`
2729}
2830
29- func expandTilde (path string ) string {
30- if strings .HasPrefix (path , "~" ) {
31- usr , err := user .Current ()
32- if err != nil {
33- return path
34- }
35- return strings .Replace (path , "~" , usr .HomeDir , 1 )
31+ func expandTilde (path string , homeDir string ) string {
32+ if strings .HasPrefix (path , "~/" ) {
33+ return filepath .Join (homeDir , path [2 :])
3634 }
3735 return path
3836}
3937
40- func readConfig (filePath string ) ([]string , []ui.System , error ) {
41- localFile , err := os .ReadFile (filePath )
42- if err != nil {
43- os .Exit (1 )
44- }
45- t := T {}
46- err = yaml .Unmarshal (localFile , & t )
38+ func readConfig (configBytes []byte ) ([]string , []types.System , error ) {
39+ cfg := Config {}
40+ err := yaml .Unmarshal (configBytes , & cfg )
4741 if err != nil {
4842 return nil , nil , err
4943 }
5044
51- systems := make ([]ui .System , 0 )
45+ var systems []types .System
5246
53- for _ , system := range t .Systems {
47+ for _ , system := range cfg .Systems {
5448 for _ , env := range system .Envs {
5549
56- var awsConfigType ui .AWSConfigSourceType
50+ var awsConfigType types .AWSConfigSourceType
5751 var awsConfigSource string
5852 switch {
5953 case env .AwsConfigSource == "default" :
60- awsConfigType = ui .DefaultCfgType
54+ awsConfigType = types .DefaultCfgType
6155 case strings .HasPrefix (env .AwsConfigSource , "profile:::" ):
6256 configElements := strings .Split (env .AwsConfigSource , "profile:::" )
6357 awsConfigSource = configElements [len (configElements )- 1 ]
64- awsConfigType = ui .SharedCfgProfileType
58+ awsConfigType = types .SharedCfgProfileType
6559 case strings .HasPrefix (env .AwsConfigSource , "assume-role:::" ):
6660 configElements := strings .Split (env .AwsConfigSource , "assume-role:::" )
6761 awsConfigSource = configElements [len (configElements )- 1 ]
68- awsConfigType = ui .AssumeRoleCfgType
62+ awsConfigType = types .AssumeRoleCfgType
6963 default :
7064 return nil ,
7165 nil ,
72- fmt .Errorf ("system with key %s doesn't have a valid aws-config-source for env %s" ,
66+ fmt .Errorf ("%w: system: %s env: %s" , errInvalidConfigSourceProvided ,
7367 system .Key ,
7468 env .Name )
7569 }
76- systems = append (systems , ui .System {
70+ systems = append (systems , types .System {
7771 Key : system .Key ,
7872 Env : env .Name ,
7973 AWSConfigSourceType : awsConfigType ,
@@ -85,6 +79,5 @@ func readConfig(filePath string) ([]string, []ui.System, error) {
8579 })
8680 }
8781 }
88- return t .EnvSequence , systems , err
89-
82+ return cfg .EnvSequence , systems , nil
9083}
0 commit comments