Skip to content

Commit

Permalink
feat: sync .env file
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jul 10, 2024
1 parent a02c08f commit 32fd490
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
1 change: 1 addition & 0 deletions messages/deploy/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
EdgeApplicationDeployPathFlag = "Path to where your static files are stored"
EdgeApplicationDeployProjectConfFlag = "Relative path to where your custom azion.json and args.json files are stored"
EdgeApplicationDeploySync = "Synchronizes the local azion.json file with remote resources"
EnvFlag = "Relative path to where your custom .env file is stored"
OriginsSuccessful = "Created Origin for Edge Application\n"
OriginsUpdateSuccessful = "Updated Origin for Edge Application %v with ID %v \n"
CacheSettingsSuccessful = "Created Cache Settings for Edge Application\n"
Expand Down
2 changes: 2 additions & 0 deletions messages/sync/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const (
SYNCMESSAGERULE = "Adding out of sync rule '%s' to your azion.json file\n"
SYNCMESSAGECACHE = "Adding out of sync cache '%s' to your azion.json file\n"
SYNCMESSAGEORIGIN = "Adding out of sync origin '%s' to your azion.json file\n"
SYNCMESSAGEENV = "Adding out of sync variable '%s' to your azion account\n"
HELPFLAG = "Displays more information about the sync command"
CONFDIRFLAG = "Relative path to where your custom azion.json and args.json files are stored"
ENVFLAG = "Relative path to where your custom .env file is stored"
)
8 changes: 6 additions & 2 deletions pkg/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var (
SkipBuild bool
ProjectConf string
Sync bool
Env string
)

func NewDeployCmd(f *cmdutil.Factory) *DeployCmd {
Expand Down Expand Up @@ -95,6 +96,7 @@ func NewCobraCmd(deploy *DeployCmd) *cobra.Command {
deployCmd.Flags().BoolVar(&SkipBuild, "skip-build", false, msg.DeployFlagSkipBuild)
deployCmd.Flags().StringVar(&ProjectConf, "config-dir", "azion", msg.EdgeApplicationDeployProjectConfFlag)
deployCmd.Flags().BoolVar(&Sync, "sync", false, msg.EdgeApplicationDeploySync)
deployCmd.Flags().StringVar(&Env, "env", ".edge/.env", msg.EnvFlag)
return deployCmd
}

Expand All @@ -109,7 +111,7 @@ func (cmd *DeployCmd) ExternalRun(f *cmdutil.Factory, configPath string) error {

func (cmd *DeployCmd) Run(f *cmdutil.Factory) error {
msgs := []string{}
logger.FInfoFlags(cmd.F.IOStreams.Out, "Running deploy command", cmd.F.Format, cmd.F.Out)
logger.FInfoFlags(cmd.F.IOStreams.Out, "Running deploy command\n", cmd.F.Format, cmd.F.Out)
msgs = append(msgs, "Running deploy command")
ctx := context.Background()

Expand All @@ -120,7 +122,9 @@ func (cmd *DeployCmd) Run(f *cmdutil.Factory) error {

if Sync {
sync.ProjectConf = ProjectConf
if err := sync.Sync(sync.NewSync(f)); err != nil {
syncCmd := sync.NewSync(f)
syncCmd.EnvPath = Env
if err := sync.Sync(syncCmd); err != nil {
logger.Debug("Error while synchronizing local resources with remove resources", zap.Error(err))
return err
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/cmd/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import (
"go.uber.org/zap"
)

var ProjectConf string
var (
ProjectConf string
)

type SyncCmd struct {
Io *iostreams.IOStreams
GetAzionJsonContent func(confPath string) (*contracts.AzionApplicationOptions, error)
WriteAzionJsonContent func(conf *contracts.AzionApplicationOptions, confPath string) error
F *cmdutil.Factory
SyncResources func(f *cmdutil.Factory, info contracts.SyncOpts, synch *SyncCmd) error
EnvPath string
}

func NewSync(f *cmdutil.Factory) *SyncCmd {
Expand Down Expand Up @@ -50,6 +53,7 @@ func NewCmd(f *cmdutil.Factory) *cobra.Command {
}
syncCmd.Flags().BoolP("help", "h", false, msg.HELPFLAG)
syncCmd.Flags().StringVar(&ProjectConf, "config-dir", "azion", msg.CONFDIRFLAG)
syncCmd.Flags().StringVar(&cmdFactory.EnvPath, "env", ".edge/.env", msg.ENVFLAG)
return syncCmd
}

Expand Down
43 changes: 43 additions & 0 deletions pkg/cmd/sync/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
msg "github.com/aziontech/azion-cli/messages/sync"
edgeApp "github.com/aziontech/azion-cli/pkg/api/edge_applications"
"github.com/aziontech/azion-cli/pkg/api/origin"
varApi "github.com/aziontech/azion-cli/pkg/api/variables"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/joho/godotenv"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -40,6 +42,11 @@ func SyncLocalResources(f *cmdutil.Factory, info contracts.SyncOpts, synch *Sync
return fmt.Errorf(msg.ERRORSYNC, err.Error())
}

err = synch.syncEnv(f)
if err != nil {
return fmt.Errorf(msg.ERRORSYNC, err.Error())
}

return nil
}

Expand Down Expand Up @@ -125,3 +132,39 @@ func (synch *SyncCmd) syncRules(info contracts.SyncOpts, f *cmdutil.Factory) err
}
return nil
}

func (synch *SyncCmd) syncEnv(f *cmdutil.Factory) error {

client := varApi.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))
resp, err := client.List(context.Background())
if err != nil {
return err
}

// Load the .env file
envs, err := godotenv.Read(synch.EnvPath)
if err != nil {
logger.Debug("Error while loading .env file", zap.Error(err))
return err
}

for _, variable := range resp {
if v := envs[variable.GetKey()]; v != "" {
delete(envs, variable.GetKey())
}
}

for key, value := range envs {
createReq := &varApi.Request{}
createReq.Key = key
createReq.Value = value
_, err := client.Create(ctx, *createReq)
if err != nil {
logger.Debug("Error while creating variables during sync process", zap.Error(err))
return err
}
logger.FInfoFlags(
synch.Io.Out, fmt.Sprintf(msg.SYNCMESSAGEENV, key), synch.F.Format, synch.F.Out)
}
return nil
}
4 changes: 3 additions & 1 deletion pkg/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func TestGetVersionGitHub(t *testing.T) {
defer func() { ApiURL = oldURL }()

gh := NewGithub()
gh.GetVersionGitHub = getVersionGitHub
gh.GetVersionGitHub = func(name string) (string, error) {
return tt.wantTag, nil
}

gotTag, err := gh.GetVersionGitHub(tt.repoName)
if (err != nil) != tt.wantErr {
Expand Down

0 comments on commit 32fd490

Please sign in to comment.