Skip to content

Commit

Permalink
adding init flow
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamvernekar committed Nov 15, 2023
1 parent 1ff2bdc commit 95fff87
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 28 deletions.
94 changes: 73 additions & 21 deletions cmd/dolores/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ func NewInitCommand(newCli GetClient) *cli.Command {
}

type Input struct {
CloudProvider string `survey:"cloud_provider"`
UserID string `survey:"user_id"`
Bucket string
Location string
ApplicationCredentials string `survey:"google_creds"`
ApplicationCredentials string `survey:"creds"`
}

func (inp Input) ToMetadata(env string) config.Metadata {
return config.Metadata{
CloudProvider: inp.CloudProvider,
Bucket: inp.Bucket,
Location: inp.Location,
CreatedAt: time.Now(),
Expand All @@ -57,13 +59,78 @@ func (inp Input) ToMetadata(env string) config.Metadata {
}
}

func (c *InitCommand) getCred(res *Input) error {
qs := []*survey.Question{}

switch res.CloudProvider {
case config.GCS:
{
credFile := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if credFile != "" {
qs = append(qs, &survey.Question{
Name: "creds",
Validate: survey.Required,
Prompt: &survey.Select{
Message: "Use GOOGLE_APPLICATION_CREDENTIALS env as credentials file",
Options: []string{credFile},
},
})
} else {
qs = append(qs, &survey.Question{
Name: "creds",
Prompt: &survey.Input{
Message: "Enter google service account file path",
},
Validate: survey.Required,
})
}
}
case config.AWS:
{
credFile := os.Getenv("AWS_APPLICATION_CREDENTIALS")
if credFile != "" {
qs = append(qs, &survey.Question{
Name: "creds",
Validate: survey.Required,
Prompt: &survey.Select{
Message: "Use AWS_APPLICATION_CREDENTIALS env as credentials file",
Options: []string{credFile},
},
})
} else {
qs = append(qs, &survey.Question{
Name: "creds",
Prompt: &survey.Input{
Message: "Enter aws service account file path",
},
Validate: survey.Required,
})
}
}
}

credRes := new(Input)
if err := survey.Ask(qs, credRes); err != nil {
return fmt.Errorf("failed to get appropriate input: %w", err)
}
res.ApplicationCredentials = credRes.ApplicationCredentials
return nil
}

func (c *InitCommand) getData(env string) (*Input, error) {
credFile := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
qs := []*survey.Question{
{
Name: "cloud_provider",
Validate: survey.Required,
Prompt: &survey.Select{
Message: "Select Cloud provider",
Options: []string{config.AWS, config.GCS},
},
},
{
Name: "bucket",
Prompt: &survey.Input{
Message: "Enter the GCS bucket name:",
Message: "Enter the bucket name:",
},
Validate: survey.Required,
},
Expand All @@ -84,28 +151,13 @@ func (c *InitCommand) getData(env string) (*Input, error) {
},
},
}
if credFile != "" {
qs = append(qs, &survey.Question{
Name: "google_creds",
Validate: survey.Required,
Prompt: &survey.Select{
Message: "Use GOOGLE_APPLICATION_CREDENTIALS env as credentials file",
Options: []string{credFile},
},
})
} else {
qs = append(qs, &survey.Question{
Name: "google_creds",
Prompt: &survey.Input{
Message: "Enter google service account file path",
},
Validate: survey.Required,
})
}
res := new(Input)
if err := survey.Ask(qs, res); err != nil {
return nil, fmt.Errorf("failed to get appropriate input: %w", err)
}
if err := c.getCred(res); err != nil {
return nil, fmt.Errorf("failed to get appropriate input: %w", err)
}
return res, nil
}

Expand Down
9 changes: 8 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Cloud struct {
}

type Metadata struct {
CloudProvider string `json:"cloud_provider"`
Bucket string `json:"bucket"`
Location string `json:"location"`
Environment string `json:"environment"`
Expand All @@ -57,6 +58,9 @@ func (c Client) BucketName() string {
}

func (c Client) Valid() error {
if c.Provider == "" {
return ErrCloudProviderNotFound
}
if c.Cloud.ApplicationCredentials == "" {
return ErrInvalidGoogleCreds
}
Expand All @@ -77,7 +81,10 @@ func LoadClient(ctx context.Context, env string) (Client, error) {
}

md := d.Environments[env].Metadata
cfg.Provider = d.Environments[env].CloudProvider
if cloudProvider := md.CloudProvider; cloudProvider != "" {
cfg.Provider = cloudProvider
}

if cfg.Cloud.ApplicationCredentials == "" {
if creds := md.ApplicationCredentials; creds != "" {
cfg.Cloud.ApplicationCredentials = creds
Expand Down
10 changes: 4 additions & 6 deletions config/dolores.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (
var ErrInvalidDoloresConfig = errors.New("invalid dolores config")

type Environment struct {
Metadata `json:"metadata"`
KeyFile string `json:"key_file"`
CloudProvider string `json:"cloud_provider"`
Metadata `json:"metadata"`
KeyFile string `json:"key_file"`
}

type Dolores struct {
Expand All @@ -26,9 +25,8 @@ func (d *Dolores) AddEnvironment(env string, keyFile string, md Metadata) {
d.Environments = make(map[string]Environment)
}
d.Environments[env] = Environment{
Metadata: md,
KeyFile: keyFile,
CloudProvider: AWS, // Temp adding for testing
Metadata: md,
KeyFile: keyFile,
}
}

Expand Down

0 comments on commit 95fff87

Please sign in to comment.