Skip to content

Commit

Permalink
chore: add option for multiple config paths
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jun 18, 2024
1 parent cc2b076 commit 93bf903
Show file tree
Hide file tree
Showing 24 changed files with 114 additions and 96 deletions.
1 change: 1 addition & 0 deletions messages/build/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ var (
FlagPolyfill = "Use node polyfills in build"
FlagEntry = "Code entrypoint; (default: ./main.js)"
IsFirewall = "Indicates whether the function to be run is intended for the Edge Firewall"
ProjectConfFlag = "Relative path to where your custom azion.json and args.json files are stored"
)
1 change: 1 addition & 0 deletions messages/delete/edge_application/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ var (
CascadeSuccess = "Remote resources deleted successfully"
FlagId = "Unique identifier of the Edge Application"
AskInput = "Enter the ID of the Edge Application you wish to delete:"
CONFDIRFLAG = "Relative path to where your custom azion.json and args.json files are stored"
)
71 changes: 36 additions & 35 deletions messages/deploy/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,42 @@ package deploy

var (
// deploy cmd
DeployUsage = "deploy"
DeployShortDescription = "Deploys an Edge Application"
DeployLongDescription = "Deploys an Edge Application"
DeploySuccessful = "Your Edge Application was deployed successfully\n"
SimpleDeploySuccessful = "Your simple Edge Application was deployed successfully\n"
DeployOutputDomainSuccess = "\nTo visualize your application access the Domain: %v\n"
EdgeApplicationDeployDomainHint = "You may now edit your Domain and add your own CNAMES. To do this you may run 'azion domain update' command and also configure your DNS\n"
DeployOutputCachePurge = "Domain cache was purged\n"
DeployOutputEdgeFunctionCreate = "Created Edge Function %v with ID %v\n"
DeployOutputEdgeFunctionUpdate = "Updated Edge Function %v with ID %v\n"
DeployOutputEdgeApplicationCreate = "Created Edge Application %v with ID %v\n"
DeployOutputEdgeApplicationUpdate = "Updated Edge Application %v with ID %v\n"
DeployOutputDomainCreate = "Created Domain %v with ID %v\n"
DeployOutputDomainUpdate = "Updated Domain %v with ID %v\n"
EdgeApplicationDeployPathFlag = "Path to where your static files are 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"
BucketSuccessful = "Created Bucket %s\n"
RulesEngineSuccessful = "Created Rules Engine for Edge Application\n"
DeployFlagHelp = "Displays more information about the deploy command"
DeployFlagAuto = "If sent, the entire flow of the command will be run without interruptions"
DeployFlagNoPrompt = "If sent, whenever the CLI would display an interactive prompt due to an error, it instead just returns the error"
DeployFlagSkipBuild = "If sent, the build command will not be called during the deploy process"
DeployPropagation = "Your application is being deployed to all Azion Edge Locations and it might take a few minutes.\n"
UploadStart = "Uploading static files\n"
UploadSuccessful = "\nUpload completed successfully!\n"
BucketInUse = "This bucket's name is already in use, please try another one\n"
AppInUse = "This Edge Application's name is already in use, please try another one\n"
DomainInUse = "This domain's name is already in use, please try another one\n"
FuncInUse = "This Edge Function's name is already in use, please try another one\n"
FuncInstInUse = "This function instance's name is already in use, please try another one\n"
AskInputName = "Type the new name:"
ProjectNameMessage = "Using the same name as your project to create the bucket\n"
AskCreateCacheSettings = `Azion CLI offers to create the following Cache Settings specifications:
DeployUsage = "deploy"
DeployShortDescription = "Deploys an Edge Application"
DeployLongDescription = "Deploys an Edge Application"
DeploySuccessful = "Your Edge Application was deployed successfully\n"
SimpleDeploySuccessful = "Your simple Edge Application was deployed successfully\n"
DeployOutputDomainSuccess = "\nTo visualize your application access the Domain: %v\n"
EdgeApplicationDeployDomainHint = "You may now edit your Domain and add your own CNAMES. To do this you may run 'azion domain update' command and also configure your DNS\n"
DeployOutputCachePurge = "Domain cache was purged\n"
DeployOutputEdgeFunctionCreate = "Created Edge Function %v with ID %v\n"
DeployOutputEdgeFunctionUpdate = "Updated Edge Function %v with ID %v\n"
DeployOutputEdgeApplicationCreate = "Created Edge Application %v with ID %v\n"
DeployOutputEdgeApplicationUpdate = "Updated Edge Application %v with ID %v\n"
DeployOutputDomainCreate = "Created Domain %v with ID %v\n"
DeployOutputDomainUpdate = "Updated Domain %v with ID %v\n"
EdgeApplicationDeployPathFlag = "Path to where your static files are stored"
EdgeApplicationDeployProjectConfFlag = "Relative path to where your custom azion.json and args.json files are 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"
BucketSuccessful = "Created Bucket %s\n"
RulesEngineSuccessful = "Created Rules Engine for Edge Application\n"
DeployFlagHelp = "Displays more information about the deploy command"
DeployFlagAuto = "If sent, the entire flow of the command will be run without interruptions"
DeployFlagNoPrompt = "If sent, whenever the CLI would display an interactive prompt due to an error, it instead just returns the error"
DeployFlagSkipBuild = "If sent, the build command will not be called during the deploy process"
DeployPropagation = "Your application is being deployed to all Azion Edge Locations and it might take a few minutes.\n"
UploadStart = "Uploading static files\n"
UploadSuccessful = "\nUpload completed successfully!\n"
BucketInUse = "This bucket's name is already in use, please try another one\n"
AppInUse = "This Edge Application's name is already in use, please try another one\n"
DomainInUse = "This domain's name is already in use, please try another one\n"
FuncInUse = "This Edge Function's name is already in use, please try another one\n"
FuncInstInUse = "This function instance's name is already in use, please try another one\n"
AskInputName = "Type the new name:"
ProjectNameMessage = "Using the same name as your project to create the bucket\n"
AskCreateCacheSettings = `Azion CLI offers to create the following Cache Settings specifications:
- Browser Cache Settings: Override Cache Settings
- Maximum TTL for Browser Cache Settings (in seconds): 7200
- Edge Application Cache Settings: Override Cache Settings
Expand Down
3 changes: 2 additions & 1 deletion messages/link/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ var (
)

const (
FLAG_REMOTE = "Clones a remote repository to be linked to an Azion Edge Application"
FLAG_REMOTE = "Clones a remote repository to be linked to an Azion Edge Application"
FLAGPATHCONF = "Relative path to where your custom azion.json and args.json files are stored"
)
1 change: 1 addition & 0 deletions messages/sync/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ const (
LONGDESCRIPTION = "Synchronizes your local file containing your existing application resources configuration with remote resources"
SYNCMESSAGERULE = "Adding out of sync rule '%s' to your azion.json file\n"
HELPFLAG = "Displays more information about the sync command"
CONFDIRFLAG = "Relative path to where your custom azion.json and args.json files are stored"
)
5 changes: 3 additions & 2 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type BuildCmd struct {
CommandRunner func(f *cmdutil.Factory, comm string, envVars []string) (string, error)
FileReader func(path string) ([]byte, error)
ConfigRelativePath string
GetAzionJsonContent func() (*contracts.AzionApplicationOptions, error)
WriteAzionJsonContent func(conf *contracts.AzionApplicationOptions) error
GetAzionJsonContent func(pathConf string) (*contracts.AzionApplicationOptions, error)
WriteAzionJsonContent func(conf *contracts.AzionApplicationOptions, confPath string) error
EnvLoader func(path string) ([]string, error)
Stat func(path string) (fs.FileInfo, error)
GetWorkDir func() (string, error)
Expand Down Expand Up @@ -56,6 +56,7 @@ func NewCobraCmd(build *BuildCmd) *cobra.Command {
buildCmd.Flags().StringVar(&fields.NodePolyfills, "use-node-polyfills", "", msg.FlagPolyfill)
buildCmd.Flags().StringVar(&fields.OwnWorker, "use-own-worker", "", msg.FlagWorker)
buildCmd.Flags().BoolVar(&fields.IsFirewall, "firewall", false, msg.IsFirewall)
buildCmd.Flags().StringVar(&fields.ProjectPath, "--config-dir", "azion", msg.ProjectConfFlag)

return buildCmd
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/build/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (cmd *BuildCmd) run(fields *contracts.BuildInfo) error {
func RunBuildCmdLine(cmd *BuildCmd, fields *contracts.BuildInfo) error {
var err error

conf, err := cmd.GetAzionJsonContent()
conf, err := cmd.GetAzionJsonContent(fields.ProjectPath)
if err != nil {
logger.Debug("Error while building your project", zap.Error(err))
return msg.ErrorBuilding
Expand Down Expand Up @@ -64,6 +64,6 @@ func RunBuildCmdLine(cmd *BuildCmd, fields *contracts.BuildInfo) error {
vulcanParams += " --firewall "
}

return vulcan(cmd, conf, vulcanParams)
return vulcan(cmd, conf, vulcanParams, fields)

}
4 changes: 2 additions & 2 deletions pkg/cmd/build/vulcan.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"go.uber.org/zap"
)

func vulcan(cmd *BuildCmd, conf *contracts.AzionApplicationOptions, vulcanParams string) error {
func vulcan(cmd *BuildCmd, conf *contracts.AzionApplicationOptions, vulcanParams string, fields *contracts.BuildInfo) error {
// checking if vulcan major is correct
vulcanVer, err := cmd.CommandRunner(cmd.f, "npm show edge-functions version", []string{})
if err != nil {
Expand All @@ -31,7 +31,7 @@ func vulcan(cmd *BuildCmd, conf *contracts.AzionApplicationOptions, vulcanParams
return fmt.Errorf(msg.ErrorVulcanExecute.Error(), err.Error())
}

err = cmd.WriteAzionJsonContent(conf)
err = cmd.WriteAzionJsonContent(conf, fields.ProjectPath)
if err != nil {
logger.Debug("Error while writing azion.json file", zap.Error(err))
return utils.ErrorWritingAzionJsonFile
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/delete/edge_application/cascade.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (del *DeleteCmd) Cascade(ctx context.Context) error {
azionJson, err := del.GetAzion()
azionJson, err := del.GetAzion(ProjectConf)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return msg.ErrorMissingAzionJson
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/delete/edge_application/edge_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import (
"go.uber.org/zap"
)

var ProjectConf string

type DeleteCmd struct {
Io *iostreams.IOStreams
GetAzion func() (*contracts.AzionApplicationOptions, error)
GetAzion func(confPath string) (*contracts.AzionApplicationOptions, error)
f *cmdutil.Factory
UpdateJson func(cmd *DeleteCmd) error
}
Expand Down Expand Up @@ -61,6 +63,7 @@ func NewCobraCmd(delete *DeleteCmd) *cobra.Command {
cmd.Flags().Int64Var(&application_id, "application-id", 0, msg.FlagId)
cmd.Flags().Bool("cascade", true, msg.CascadeFlag)
cmd.Flags().BoolP("help", "h", false, msg.HelpFlag)
cmd.Flags().StringVar(&ProjectConf, "--config-dir", "azion", msg.CONFDIRFLAG)

return cmd
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/delete/edge_application/edge_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestCreate(t *testing.T) {
f, stdout, _ := testutils.NewFactory(mock)

del := NewDeleteCmd(f)
del.GetAzion = func() (*contracts.AzionApplicationOptions, error) {
del.GetAzion = func(confPath string) (*contracts.AzionApplicationOptions, error) {
return options, nil
}
del.UpdateJson = func(cmd *DeleteCmd) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/deploy/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (cmd *DeployCmd) doBucket(client *api.Client, ctx context.Context, conf *co
}

logger.FInfo(cmd.Io.Out, fmt.Sprintf(msg.BucketSuccessful, conf.Bucket))
return cmd.WriteAzionJsonContent(conf)
return cmd.WriteAzionJsonContent(conf, ProjectConf)
}

func askForInput(msg string, defaultIn string) (string, error) {
Expand Down
20 changes: 11 additions & 9 deletions pkg/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type DeployCmd struct {
GetWorkDir func() (string, error)
FileReader func(path string) ([]byte, error)
WriteFile func(filename string, data []byte, perm fs.FileMode) error
GetAzionJsonContent func() (*contracts.AzionApplicationOptions, error)
WriteAzionJsonContent func(conf *contracts.AzionApplicationOptions) error
GetAzionJsonContent func(pathConfig string) (*contracts.AzionApplicationOptions, error)
WriteAzionJsonContent func(conf *contracts.AzionApplicationOptions, confConf string) error
EnvLoader func(path string) ([]string, error)
BuildCmd func(f *cmdutil.Factory) *build.BuildCmd
Open func(name string) (*os.File, error)
Expand All @@ -43,10 +43,11 @@ type DeployCmd struct {
}

var (
Path string
Auto bool
NoPrompt bool
SkipBuild bool
Path string
Auto bool
NoPrompt bool
SkipBuild bool
ProjectConf string
)

func NewDeployCmd(f *cmdutil.Factory) *DeployCmd {
Expand Down Expand Up @@ -89,6 +90,7 @@ func NewCobraCmd(deploy *DeployCmd) *cobra.Command {
deployCmd.Flags().BoolVar(&Auto, "auto", false, msg.DeployFlagAuto)
deployCmd.Flags().BoolVar(&NoPrompt, "no-prompt", false, msg.DeployFlagNoPrompt)
deployCmd.Flags().BoolVar(&SkipBuild, "skip-build", false, msg.DeployFlagSkipBuild)
deployCmd.Flags().StringVar(&ProjectConf, "config-dir", "azion", msg.EdgeApplicationDeployProjectConfFlag)
return deployCmd
}

Expand All @@ -114,7 +116,7 @@ func (cmd *DeployCmd) Run(f *cmdutil.Factory) error {
}
}

conf, err := cmd.GetAzionJsonContent()
conf, err := cmd.GetAzionJsonContent(ProjectConf)
if err != nil {
logger.Debug("Failed to get Azion JSON content", zap.Error(err))
return err
Expand All @@ -124,7 +126,7 @@ func (cmd *DeployCmd) Run(f *cmdutil.Factory) error {

conf.Prefix = versionID

err = checkArgsJson(cmd)
err = checkArgsJson(cmd, ProjectConf)
if err != nil {
return err
}
Expand Down Expand Up @@ -223,7 +225,7 @@ func (cmd *DeployCmd) Run(f *cmdutil.Factory) error {
}
}

err = interpreter.CreateResources(conf, manifestStructure, f)
err = interpreter.CreateResources(conf, manifestStructure, f, ProjectConf)
if err != nil {
return err
}
Expand Down
20 changes: 11 additions & 9 deletions pkg/cmd/deploy/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"path"
"strconv"
"strings"

Expand Down Expand Up @@ -57,7 +58,7 @@ func (cmd *DeployCmd) doFunction(clients *Clients, ctx context.Context, conf *co
conf.Function.ID = functionId
}

err = cmd.WriteAzionJsonContent(conf)
err = cmd.WriteAzionJsonContent(conf, ProjectConf)
if err != nil {
logger.Debug("Error while writing azion.json file", zap.Error(err))
return err
Expand Down Expand Up @@ -85,7 +86,7 @@ func (cmd *DeployCmd) doFunction(clients *Clients, ctx context.Context, conf *co
conf.Function.InstanceID = instance.GetId()
break
}
err = cmd.WriteAzionJsonContent(conf)
err = cmd.WriteAzionJsonContent(conf, ProjectConf)
if err != nil {
logger.Debug("Error while writing azion.json file", zap.Error(err))
return err
Expand Down Expand Up @@ -137,7 +138,7 @@ func (cmd *DeployCmd) doApplication(client *apiapp.Client, ctx context.Context,
break
}

err := cmd.WriteAzionJsonContent(conf)
err := cmd.WriteAzionJsonContent(conf, ProjectConf)
if err != nil {
logger.Debug("Error while writing azion.json file", zap.Error(err))
return err
Expand Down Expand Up @@ -191,7 +192,7 @@ func (cmd *DeployCmd) doDomain(client *apidom.Client, ctx context.Context, conf
break
}

err = cmd.WriteAzionJsonContent(conf)
err = cmd.WriteAzionJsonContent(conf, ProjectConf)
if err != nil {
logger.Debug("Error while writing azion.json file", zap.Error(err))
return err
Expand Down Expand Up @@ -597,18 +598,19 @@ func checkToken(f *cmdutil.Factory) error {
return nil
}

func checkArgsJson(cmd *DeployCmd) error {
func checkArgsJson(cmd *DeployCmd, projectPath string) error {
workingDir, err := cmd.GetWorkDir()
if err != nil {
return err
}

workDirPath := workingDir + "/azion/args.json"
_, err = cmd.FileReader(workDirPath)
workingDirPath := path.Join(workingDir, projectPath, "args.json")

_, err = cmd.FileReader(workingDirPath)
if err != nil {
if err := cmd.WriteFile(workDirPath, []byte("{}"), 0644); err != nil {
if err := cmd.WriteFile(workingDirPath, []byte("{}"), 0644); err != nil {
logger.Debug("Error while trying to create args.json file", zap.Error(err))
return fmt.Errorf(utils.ErrorCreateFile.Error(), workDirPath)
return fmt.Errorf(utils.ErrorCreateFile.Error(), workingDirPath)
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type LinkInfo struct {
GlobalFlagAll bool
remote string
Auto bool
projectPath string
}

type LinkCmd struct {
Expand Down Expand Up @@ -119,6 +120,7 @@ func NewCobraCmd(link *LinkCmd, f *cmdutil.Factory) *cobra.Command {
cobraCmd.Flags().StringVar(&info.Mode, "mode", "", msg.EdgeApplicationsLinkFlagMode)
cobraCmd.Flags().BoolVar(&info.Auto, "auto", false, msg.LinkFlagAuto)
cobraCmd.Flags().StringVar(&info.remote, "remote", "", msg.FLAG_REMOTE)
cobraCmd.Flags().StringVar(&info.projectPath, "config-dir", "azion", msg.FLAGPATHCONF)

return cobraCmd
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/cmd/link/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package link

import (
"encoding/json"
"path"
"strings"

msg "github.com/aziontech/azion-cli/messages/init"
Expand All @@ -11,22 +12,22 @@ import (

func (cmd *LinkCmd) createTemplateAzion(info *LinkInfo) error {

err := cmd.Mkdir(info.PathWorkingDir+"/azion", 0755) // 0755 is the permission mode for the new directories
err := cmd.Mkdir(path.Join(info.PathWorkingDir, info.projectPath), 0755) // 0755 is the permission mode for the new directories
if err != nil {
return msg.ErrorFailedCreatingAzionDirectory
}

azionJson := &contracts.AzionApplicationOptions{
Name: info.Name,
Env: "production",
Name: info.Name,
Env: "production",
Preset: strings.ToLower(info.Preset),
Mode: strings.ToLower(info.Mode),
Prefix: "",
Mode: strings.ToLower(info.Mode),
Prefix: "",
}
azionJson.Function.Name = "__DEFAULT__"
azionJson.Function.InstanceName = "__DEFAULT__"
azionJson.Function.File = "./out/worker.js"
azionJson.Function.Args = "./azion/args.json"
azionJson.Function.Args = path.Join(info.projectPath, "args.json")
azionJson.Domain.Name = "__DEFAULT__"
azionJson.Application.Name = "__DEFAULT__"
azionJson.RtPurge.PurgeOnPublish = true
Expand All @@ -41,7 +42,7 @@ func (cmd *LinkCmd) createJsonFile(options *contracts.AzionApplicationOptions, i
return msg.ErrorUnmarshalAzionFile
}

err = cmd.WriteFile(info.PathWorkingDir+"/azion/azion.json", data, 0644)
err = cmd.WriteFile(path.Join(info.PathWorkingDir, info.projectPath, "azion.json"), data, 0644)
if err != nil {
return utils.ErrorInternalServerError
}
Expand Down
Loading

0 comments on commit 93bf903

Please sign in to comment.