Skip to content

Commit

Permalink
remove repo from flags, always use repo root as build root
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Dec 10, 2023
1 parent 35a2df1 commit 0dfe9b7
Show file tree
Hide file tree
Showing 23 changed files with 43 additions and 80 deletions.
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ and release your KubeFox Apps.
}

func init() {
rootCmd.PersistentFlags().StringVarP(&cfg.Flags.RepoPath, "repo", "r", "", "path to directory containing Git repository")
rootCmd.PersistentFlags().StringVarP(&cfg.Flags.AppPath, "app", "a", "", "path to directory containing KubeFox App")
rootCmd.PersistentFlags().StringVarP(&cfg.Flags.OutFormat, "output", "o", "yaml", `output format, one of ["json", "yaml"]`)
rootCmd.PersistentFlags().BoolVarP(&cfg.Flags.Info, "info", "i", false, "enable info output")
Expand Down
1 change: 0 additions & 1 deletion docs/fox.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ and release your KubeFox Apps.
-h, --help help for fox
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ fox build my-component --publish
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ See each sub-command's help for details on how to use the generated script.
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_completion_bash.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ fox completion bash
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_completion_fish.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ fox completion fish [flags]
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_completion_powershell.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ fox completion powershell [flags]
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_completion_zsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ fox completion zsh [flags]
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Use the config subcommand to help setup your local environment.
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_config_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fox config setup [flags]
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_config_show.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fox config show [flags]
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ fox deploy [NAME] [flags]
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ fox docs [flags]
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_init.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ fox init [flags]
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ fox proxy 8080 --virtual-env my-env --app-deployment my-dep
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ fox publish [NAME] [flags]
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ fox release v1.2.3 --virtual-env prod --create-snapshot
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
1 change: 0 additions & 1 deletion docs/fox_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fox version [flags]
-a, --app string path to directory containing KubeFox App
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand Down
24 changes: 13 additions & 11 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Config struct {
Kind Kind `json:"kind"`
ContainerRegistry ContainerRegistry `json:"containerRegistry"`

RepoPath string `json:"-"`
AppPath string `json:"-"`

Flags Flags `json:"-"`
Fresh bool `json:"-"`

Expand Down Expand Up @@ -122,33 +125,32 @@ func (cfg *Config) Load() {
func (cfg *Config) CleanPaths(defAppToWd bool) {
var err error

if cfg.Flags.RepoPath == "" {
cfg.Flags.RepoPath = utils.Find(".git", utils.Wd(), string(filepath.Separator))
}
cfg.Flags.RepoPath, err = filepath.Abs(cfg.Flags.RepoPath)
repoPath := utils.Find(".git", utils.Wd(), string(filepath.Separator))
cfg.RepoPath, err = filepath.Abs(repoPath)
if err != nil {
log.Fatal("Unable to resolve repo path: %v", err)
}

var appPath string
if cfg.Flags.AppPath == "" {
if defAppToWd {
cfg.Flags.AppPath = utils.Wd()
appPath = utils.Wd()
} else {
cfg.Flags.AppPath = utils.Find("app.yaml", utils.Wd(), cfg.Flags.RepoPath)
appPath = utils.Find("app.yaml", utils.Wd(), cfg.RepoPath)
}
}
cfg.Flags.AppPath, err = filepath.Abs(cfg.Flags.AppPath)
cfg.AppPath, err = filepath.Abs(appPath)
if err != nil {
log.Fatal("Unable to resolve app path: %v", err)
}

log.Verbose("Repo path: %s", cfg.Flags.RepoPath)
log.Verbose("App path: %s", cfg.Flags.AppPath)
log.Verbose("Repo path: %s", cfg.RepoPath)
log.Verbose("App path: %s", cfg.AppPath)

if cfg.Flags.AppPath == "" {
if cfg.AppPath == "" {
log.Fatal("Could not find app definition (app.yaml).")
}
if !strings.HasPrefix(cfg.Flags.AppPath, cfg.Flags.RepoPath) {
if !strings.HasPrefix(cfg.AppPath, cfg.RepoPath) {
log.Fatal("The app is not part of the Git repo.")
}
}
Expand Down
1 change: 0 additions & 1 deletion internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ type Flags struct {
// persistent flags defined in root command
AppPath string
OutFormat string
RepoPath string

DryRun bool
Info bool
Expand Down
8 changes: 4 additions & 4 deletions internal/repo/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ type DockerfileTar struct {

func (r *repo) Build(compDirName string) string {
img := r.GetCompImageFromDir(compDirName)
appYaml := r.AppYAMLBuildSubpath(compDirName, "go.mod")
appYaml := r.AppYAMLBuildSubpath()
compName := utils.CleanName(compDirName)
compDir := r.ComponentBuildSubpath(compDirName, "go.mod")
compDir := r.ComponentBuildSubpath(compDirName)
compCommit := r.GetCompCommit(compDirName).Hash.String()
rootCommit := r.GetRootCommit()
headRef := r.GetHeadRef()
Expand Down Expand Up @@ -78,7 +78,7 @@ func (r *repo) Build(compDirName string) string {
log.Verbose("Using custom Dockerfile '%s' for build", dfPath)
}

dfi, err := NewDFI(r.BuildRoot(compDirName, "go.mod"), df)
dfi, err := NewDFI(r.cfg.RepoPath, df)
if err != nil {
log.Fatal("Error creating container tar: %v", err)
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func NewDFI(path string, df []byte) (*DockerfileTar, error) {
if err != nil && !os.IsNotExist(err) {
return nil, err
}
var ignore []string
ignore := []string{".git"}
if dif != nil {
ignore, err = ignorefile.ReadAll(dif)
if err != nil {
Expand Down
21 changes: 9 additions & 12 deletions internal/repo/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,13 @@ func Init(cfg *config.Config) {
func initApp(cfg *config.Config) {
cfg.CleanPaths(true)

repoPath := cfg.Flags.RepoPath
appPath := cfg.Flags.AppPath

if cfg.Flags.Quickstart {
initDir(efs.HelloWorldPath, appPath)
initGit(repoPath, &App{}, cfg)
initDir(efs.HelloWorldPath, cfg.AppPath)
initGit(cfg.RepoPath, &App{}, cfg)
return
}

app, err := ReadApp(appPath)
app, err := ReadApp(cfg.AppPath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
log.Error("An KubeFox App definition already exists but appears to be invalid: %v.", err)
if !foxutils.YesNoPrompt("Would you like to reinitialize the app?", true) {
Expand All @@ -68,7 +65,7 @@ func initApp(cfg *config.Config) {
} else if !errors.Is(err, fs.ErrNotExist) {
log.VerboseMarshal(app, "App definition:")
log.Info("A valid KubeFox App definition already exists.")
initGit(repoPath, app, cfg)
initGit(cfg.RepoPath, app, cfg)
return
}

Expand All @@ -78,8 +75,8 @@ func initApp(cfg *config.Config) {
log.Info("To get things started quickly 🦊 Fox can create a 'hello-world' KubeFox App which")
log.Info("includes two components and example environments for testing.")
if foxutils.YesNoPrompt("Would you like to initialize the 'hello-world' KubeFox App?", false) {
initDir(efs.HelloWorldPath, appPath)
initGit(repoPath, app, cfg)
initDir(efs.HelloWorldPath, cfg.AppPath)
initGit(cfg.RepoPath, app, cfg)
return
}
log.InfoNewline()
Expand All @@ -88,12 +85,12 @@ func initApp(cfg *config.Config) {
log.Info("the app. The name is used as part of Kubernetes resource names so it must")
log.Info("contain only lowercase alpha-numeric characters and dashes. But don't worry you")
log.Info("can enter a more human friendly title and description.")
app.Name = foxutils.NamePrompt("KubeFox App", utils.CleanName(appPath), true)
app.Name = foxutils.NamePrompt("KubeFox App", utils.CleanName(cfg.AppPath), true)
app.Title = foxutils.InputPrompt("Enter the KubeFox App's title", "", false)
app.Description = foxutils.InputPrompt("Enter the KubeFox App's description", "", false)

WriteApp(appPath, app)
initGit(repoPath, app, cfg)
WriteApp(cfg.AppPath, app)
initGit(cfg.RepoPath, app, cfg)
}

func initGit(repoPath string, app *App, cfg *config.Config) {
Expand Down
51 changes: 17 additions & 34 deletions internal/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ type repo struct {
cfg *config.Config
app *App

rootPath string
appPath string

gitRepo *git.Repository
k8s *kubernetes.Client
docker *docker.Client
Expand All @@ -40,22 +37,19 @@ type App struct {
func New(cfg *config.Config) *repo {
cfg.CleanPaths(false)

repoPath := cfg.Flags.RepoPath
appPath := cfg.Flags.AppPath

if !strings.HasPrefix(appPath, repoPath) {
if !strings.HasPrefix(cfg.AppPath, cfg.RepoPath) {
log.Fatal("The app is not part of the Git repo.")
}

app, err := ReadApp(appPath)
app, err := ReadApp(cfg.AppPath)
if err != nil {
log.Fatal("Error reading the repo's 'app.yaml', try running 'fox init': %v", err)
}

log.Verbose("Opening git repo '%s'", repoPath)
gitRepo, err := git.PlainOpen(repoPath)
log.Verbose("Opening git repo '%s'", cfg.RepoPath)
gitRepo, err := git.PlainOpen(cfg.RepoPath)
if err != nil {
log.Fatal("Error opening git repo '%s': %v", repoPath, err)
log.Fatal("Error opening git repo '%s': %v", cfg.RepoPath, err)
}

d, err := docker.NewClientWithOpts(docker.FromEnv, docker.WithAPIVersionNegotiation())
Expand All @@ -64,13 +58,11 @@ func New(cfg *config.Config) *repo {
}

return &repo{
cfg: cfg,
app: app,
rootPath: repoPath,
appPath: appPath,
gitRepo: gitRepo,
k8s: kubernetes.NewClient(cfg),
docker: d,
cfg: cfg,
app: app,
gitRepo: gitRepo,
k8s: kubernetes.NewClient(cfg),
docker: d,
}
}

Expand Down Expand Up @@ -204,7 +196,7 @@ func (r *repo) GetCommit(path string) *object.Commit {
log.Fatal("Error finding commit hash: uncommitted changes present")
}

subPath := foxutils.Subpath(path, r.rootPath)
subPath := foxutils.Subpath(path, r.cfg.RepoPath)
iter, err := r.gitRepo.Log(&git.LogOptions{
PathFilter: func(c string) bool {
return strings.HasPrefix(c, subPath)
Expand All @@ -225,33 +217,24 @@ func (r *repo) GetCommit(path string) *object.Commit {
return commit
}

func (r *repo) BuildRoot(dir, buildFile string) string {
root := foxutils.Find(buildFile, r.ComponentDir(dir), r.rootPath)
if root == "" {
root = r.rootPath
}
log.Verbose("build root: %s", root)
return root
}

func (r *repo) AppYAMLBuildSubpath(dir, buildFile string) string {
return foxutils.Subpath(filepath.Join(r.appPath, "app.yaml"), r.BuildRoot(dir, buildFile))
func (r *repo) AppYAMLBuildSubpath() string {
return foxutils.Subpath(filepath.Join(r.cfg.AppPath, "app.yaml"), r.cfg.RepoPath)
}

func (r *repo) ComponentBuildSubpath(dir, buildFile string) string {
return foxutils.Subpath(r.ComponentDir(dir), r.BuildRoot(dir, buildFile))
func (r *repo) ComponentBuildSubpath(compDirName string) string {
return foxutils.Subpath(r.ComponentDir(compDirName), r.cfg.RepoPath)
}

func (r *repo) ComponentsDir() string {
return filepath.Join(r.appPath, "components")
return filepath.Join(r.cfg.AppPath, "components")
}

func (r *repo) ComponentDir(comp string) string {
return filepath.Join(r.ComponentsDir(), comp)
}

func (r *repo) ComponentRepoSubpath(comp string) string {
return foxutils.Subpath(r.ComponentDir(comp), r.rootPath)
return foxutils.Subpath(r.ComponentDir(comp), r.cfg.RepoPath)
}

func (r *repo) IsClean() bool {
Expand Down

0 comments on commit 0dfe9b7

Please sign in to comment.