Skip to content

Commit

Permalink
fly.alias support for warning supression
Browse files Browse the repository at this point in the history
  • Loading branch information
codepope committed Sep 23, 2020
1 parent 46d4145 commit 7dff5e8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cmd/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bufio"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -289,6 +290,15 @@ func requireAppName(cmd *Command) Initializer {
}

if ctx.AppConfig.AppName != "" && ctx.AppConfig.AppName != ctx.AppName {
// Quick check for a fly.alias
present, err := checkAliasFile(ctx.AppName)
if err != nil {
return err
}
if present {
return nil
}

terminal.Warnf("app flag '%s' does not match app name in config file '%s'\n", ctx.AppName, ctx.AppConfig.AppName)

if !confirm(fmt.Sprintf("Continue using '%s'", ctx.AppName)) {
Expand Down Expand Up @@ -373,6 +383,15 @@ func requireAppNameAsArg(cmd *Command) Initializer {
}

if ctx.AppConfig.AppName != "" && ctx.AppConfig.AppName != ctx.AppName {
// Quick check for a fly.alias
present, err := checkAliasFile(ctx.AppName)
if err != nil {
return err
}
if present {
return nil
}

terminal.Warnf("app flag '%s' does not match app name in config file '%s'\n", ctx.AppName, ctx.AppConfig.AppName)

if !confirm(fmt.Sprintf("Continue using '%s'", ctx.AppName)) {
Expand All @@ -385,6 +404,27 @@ func requireAppNameAsArg(cmd *Command) Initializer {
}
}

func checkAliasFile(appname string) (present bool, err error) {
if helpers.FileExists("fly.alias") {
file, err := os.Open("fly.alias")
if err != nil {
return false, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
if scanner.Text() == appname {
return true, nil
}
}

if err := scanner.Err(); err != nil {
return false, err
}
}
return false, nil
}
func workingDirectoryFromArg(index int) func(*Command) Initializer {
return func(cmd *Command) Initializer {
return Initializer{
Expand Down
11 changes: 11 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,16 @@ func runInit(commandContext *cmdctx.CmdContext) error {

fmt.Printf("New app created: %s", app.Name)

f, err := os.OpenFile("fly.alias", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}

defer f.Close()

if _, err = f.WriteString(app.Name + "\n"); err != nil {
return err
}

return nil
}

0 comments on commit 7dff5e8

Please sign in to comment.