Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent dir walking when searching for a project #106

Merged
merged 1 commit into from Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 25 additions & 20 deletions cmd/airflow.go
@@ -1,16 +1,18 @@
package cmd

import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/astronomerio/astro-cli/pkg/input"
"github.com/iancoleman/strcase"
"github.com/pkg/errors"

"github.com/astronomerio/astro-cli/messages"

"github.com/iancoleman/strcase"
"github.com/spf13/cobra"

"github.com/astronomerio/astro-cli/airflow"
Expand All @@ -20,7 +22,6 @@ import (
)

var (
projectRoot string
projectName string
forceDeploy bool

Expand Down Expand Up @@ -80,9 +81,6 @@ var (
)

func init() {
// Set up project root
projectRoot, _ = config.ProjectRoot()

// Airflow root
RootCmd.AddCommand(airflowRootCmd)

Expand All @@ -109,17 +107,14 @@ func init() {
}

func ensureProjectDir(cmd *cobra.Command, args []string) {
if !(len(projectRoot) > 0) {
if !config.IsProjectDir(config.WorkingPath) {
fmt.Println(messages.CONFIG_PROJECT_DIR_ERROR)
os.Exit(1)
}
}

// Use project name for image name
func airflowInit(cmd *cobra.Command, args []string) error {
// Grab working directory
path := fileutil.GetWorkingDir()

// Validate project name
if len(projectName) != 0 {
projectNameValid := regexp.
Expand All @@ -130,21 +125,31 @@ func airflowInit(cmd *cobra.Command, args []string) error {
return errors.New(messages.CONFIG_PROJECT_NAME_ERROR)
}
} else {
projectDirectory := filepath.Base(path)
projectDirectory := filepath.Base(config.WorkingPath)
projectName = strings.Replace(strcase.ToSnake(projectDirectory), "_", "-", -1)
}

emtpyDir := fileutil.IsEmptyDir(config.WorkingPath)
if !emtpyDir {
i, _ := input.InputConfirm(
fmt.Sprintf("%s \nYou are not in an empty directory, are you you want to initialize a project?", config.WorkingPath))

if !i {
fmt.Println("Cancelling project initialization...\n")
os.Exit(1)
}
}
exists := config.ProjectConfigExists()
if !exists {
config.CreateProjectConfig(path)
config.CreateProjectConfig(config.WorkingPath)
}
config.CFG.ProjectName.SetProjectString(projectName)
airflow.Init(path)
airflow.Init(config.WorkingPath)

if exists {
fmt.Printf(messages.CONFIG_REINIT_PROJECT_CONFIG+"\n", path)
fmt.Printf(messages.CONFIG_REINIT_PROJECT_CONFIG+"\n", config.WorkingPath)
} else {
fmt.Printf(messages.CONFIG_INIT_PROJECT_CONFIG+"\n", path)
fmt.Printf(messages.CONFIG_INIT_PROJECT_CONFIG+"\n", config.WorkingPath)
}

return nil
Expand All @@ -161,25 +166,25 @@ func airflowDeploy(cmd *cobra.Command, args []string) error {
fmt.Println(messages.REGISTRY_UNCOMMITTED_CHANGES)
return nil
}
return airflow.Deploy(projectRoot, releaseName, ws)
return airflow.Deploy(config.WorkingPath, releaseName, ws)
}

// Start an airflow cluster
func airflowStart(cmd *cobra.Command, args []string) error {
return airflow.Start(projectRoot)
return airflow.Start(config.WorkingPath)
}

// Kill an airflow cluster
func airflowKill(cmd *cobra.Command, args []string) error {
return airflow.Kill(projectRoot)
return airflow.Kill(config.WorkingPath)
}

// Stop an airflow cluster
func airflowStop(cmd *cobra.Command, args []string) error {
return airflow.Stop(projectRoot)
return airflow.Stop(config.WorkingPath)
}

// List containers of an airflow cluster
func airflowPS(cmd *cobra.Command, args []string) error {
return airflow.PS(projectRoot)
return airflow.PS(config.WorkingPath)
}
5 changes: 1 addition & 4 deletions cmd/auth.go
Expand Up @@ -8,6 +8,7 @@ import (

var (
oAuthOnly bool
domain string

authRootCmd = &cobra.Command{
Use: "auth",
Expand Down Expand Up @@ -44,8 +45,6 @@ func init() {
}

func authLogin(cmd *cobra.Command, args []string) error {
var domain string

if len(args) == 1 {
domain = args[0]
}
Expand All @@ -59,8 +58,6 @@ func authLogin(cmd *cobra.Command, args []string) error {
}

func authLogout(cmd *cobra.Command, args []string) {
var domain string

if len(args) == 1 {
domain = args[0]
} else {
Expand Down
5 changes: 1 addition & 4 deletions cmd/config.go
Expand Up @@ -38,9 +38,6 @@ var (
)

func init() {
// Set up project root
projectRoot, _ = config.ProjectRoot()

// Config root
RootCmd.AddCommand(configRootCmd)
configRootCmd.PersistentFlags().BoolVarP(&globalFlag, "global", "g", false, "view or modify global config")
Expand All @@ -53,7 +50,7 @@ func init() {
}

func ensureGlobalFlag(cmd *cobra.Command, args []string) {
if !(len(projectRoot) > 0) && !globalFlag {
if !config.IsProjectDir(config.WorkingPath) && !globalFlag {
var c = "astro config " + cmd.Use + " " + args[0] + " -g"
fmt.Printf(messages.CONFIG_USE_OUTSIDE_PROJECT_DIR, cmd.Use, cmd.Use, c)
os.Exit(1)
Expand Down
38 changes: 27 additions & 11 deletions config/config.go
Expand Up @@ -21,11 +21,16 @@ var (
// ConfigDir is the directory for astro files
ConfigDir = ".astro"

// HomeConfigPath is the path to the users global directory
HomeConfigPath = filepath.Join(fileutil.GetHomeDir(), ConfigDir)
// HomePath is the path to a users home directory
HomePath = fileutil.GetHomeDir()
// HomeConfigPath is the path to the users global config directory
HomeConfigPath = filepath.Join(HomePath, ConfigDir)
// HomeConfigFile is the global config file
HomeConfigFile = filepath.Join(HomeConfigPath, ConfigFileNameWithExt)

// WorkingPath is the path to the working directory
WorkingPath = fileutil.GetWorkingDir()

// CFGStrMap maintains string to cfg mapping
CFGStrMap = make(map[string]cfg)

Expand Down Expand Up @@ -96,22 +101,18 @@ func initProject() {
viperProject.SetConfigName(ConfigFileName)
viperProject.SetConfigType(ConfigFileType)

configPath, searchErr := fileutil.FindDirInPath(ConfigDir)
if searchErr != nil {
fmt.Printf(messages.CONFIG_SEARCH_ERROR+"\n", searchErr)
return
}

// Construct the path to the config file
projectConfigFile := filepath.Join(configPath, ConfigFileNameWithExt)
workingConfigPath := filepath.Join(WorkingPath, ConfigDir)

workingConfigFile := filepath.Join(workingConfigPath, ConfigFileNameWithExt)

// If path is empty or config file does not exist, just return
if len(configPath) == 0 || configPath == HomeConfigPath || !fileutil.Exists(projectConfigFile) {
if len(workingConfigPath) == 0 || workingConfigPath == HomeConfigPath || !fileutil.Exists(workingConfigFile) {
return
}

// Add the path we discovered
viperProject.SetConfigFile(projectConfigFile)
viperProject.SetConfigFile(workingConfigFile)

// Read in project config
readErr := viperProject.ReadInConfig()
Expand Down Expand Up @@ -162,6 +163,8 @@ func ProjectConfigExists() bool {
}

// ProjectRoot returns the path to the nearest project root
// TODO Deprecate if remains unused, removed due to
// https://github.com/astronomerio/astro-cli/issues/103
func ProjectRoot() (string, error) {
configPath, searchErr := fileutil.FindDirInPath(ConfigDir)
if searchErr != nil {
Expand All @@ -173,6 +176,19 @@ func ProjectRoot() (string, error) {
return filepath.Dir(configPath), nil
}

// IsProjectDir returns a boolean depending on if path is a valid project dir
func IsProjectDir(path string) bool {
configPath := filepath.Join(path, ConfigDir)
configFile := filepath.Join(configPath, ConfigFileNameWithExt)

// Home directory is not a project directory
if HomePath == path {
return false
}

return fileutil.Exists(configFile)
}

// saveConfig will save the config to a file
func saveConfig(v *viper.Viper, file string) error {
err := v.WriteConfigAs(file)
Expand Down
19 changes: 19 additions & 0 deletions pkg/fileutil/paths.go
Expand Up @@ -2,6 +2,7 @@ package fileutil

import (
"fmt"
"io"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -30,6 +31,8 @@ func GetHomeDir() string {
}

// FindDirInPath walks up the current directory looking for the .astro folder
// TODO Deprecate if remains unused, removed due to
// https://github.com/astronomerio/astro-cli/issues/103
func FindDirInPath(search string) (string, error) {
// Start in our current directory
workingDir := GetWorkingDir()
Expand Down Expand Up @@ -60,3 +63,19 @@ func FindDirInPath(search string) (string, error) {

return "", nil
}

// IsEmptyDir checks if path is an empty dir
func IsEmptyDir(path string) bool {
f, err := os.Open(path)
if err != nil {
fmt.Println(err)
return false
}
defer f.Close()

_, err = f.Readdirnames(1) // Or f.Readdir(1)
if err == io.EOF {
return true
}
return false // Either not empty or error, suits both cases
}