Skip to content

Commit

Permalink
refactor: use viper and configurable flags (#90)
Browse files Browse the repository at this point in the history
* use viper

* remove unused app.Parser

* refactor: combine continuous variables

* Revert "use viper"

This reverts commit 2d3005c.

* refactor: merge app and cmd/gogh(main) package

* make flags be configurable

* configurable flags

* fix save with input
  • Loading branch information
kyoh86 authored Jun 2, 2021
1 parent 6efbfc5 commit 03c8f3e
Show file tree
Hide file tree
Showing 27 changed files with 825 additions and 667 deletions.
82 changes: 0 additions & 82 deletions app/config.go

This file was deleted.

2 changes: 0 additions & 2 deletions app/doc.go

This file was deleted.

34 changes: 0 additions & 34 deletions app/servers.go

This file was deleted.

81 changes: 0 additions & 81 deletions app/setup.go

This file was deleted.

4 changes: 2 additions & 2 deletions cmd/gogh/bundle.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package main

import (
"github.com/kyoh86/gogh/v2/app"
"github.com/spf13/cobra"
)

var bundleCommand = &cobra.Command{
Use: "bundle",
Short: "Manage bundle",
PersistentPostRunE: func(*cobra.Command, []string) error {
return app.SaveServers()
return saveServers()
},
}

func init() {
setup()
facadeCommand.AddCommand(bundleCommand)
}
92 changes: 48 additions & 44 deletions cmd/gogh/bundle_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,65 @@ import (

"github.com/go-git/go-git/v5"
"github.com/kyoh86/gogh/v2"
"github.com/kyoh86/gogh/v2/app"
"github.com/spf13/cobra"
)

var bundleDumpFlags struct {
file string
type bundleDumpFlagsStruct struct {
File expandedPath `yaml:"file,omitempty"`
}

var bundleDumpCommand = &cobra.Command{
Use: "dump",
Aliases: []string{"export"},
Short: "Export current local projects",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, _ []string) error {
out := os.Stdout
if bundleDumpFlags.file != "" {
f, err := os.OpenFile(bundleDumpFlags.file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
var (
bundleDumpFlags bundleDumpFlagsStruct
bundleDumpCommand = &cobra.Command{
Use: "dump",
Aliases: []string{"export"},
Short: "Export current local projects",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, _ []string) error {
out := os.Stdout
if bundleDumpFlags.File.expanded != "" {
f, err := os.OpenFile(bundleDumpFlags.File.expanded, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
out = f
}
defer f.Close()
out = f
}
ctx := cmd.Context()
roots := app.Roots()
if len(roots) == 0 {
return nil
}
local := gogh.NewLocalController(roots[0])
if err := local.Walk(ctx, nil, func(project gogh.Project) error {
localSpec := project.Spec()
urls, err := local.GetRemoteURLs(ctx, localSpec, git.DefaultRemoteName)
if err != nil {
return err
}
uobj, err := url.Parse(urls[0])
if err != nil {
return err
ctx := cmd.Context()
list := roots()
if len(list) == 0 {
return nil
}
remoteName := strings.Join([]string{uobj.Host, strings.TrimPrefix(strings.TrimSuffix(uobj.Path, ".git"), "/")}, "/")
localName := localSpec.String()
if remoteName == localName {
fmt.Fprintln(out, localName)
local := gogh.NewLocalController(list[0])
if err := local.Walk(ctx, nil, func(project gogh.Project) error {
localSpec := project.Spec()
urls, err := local.GetRemoteURLs(ctx, localSpec, git.DefaultRemoteName)
if err != nil {
return err
}
uobj, err := url.Parse(urls[0])
if err != nil {
return err
}
remoteName := strings.Join([]string{uobj.Host, strings.TrimPrefix(strings.TrimSuffix(uobj.Path, ".git"), "/")}, "/")
localName := localSpec.String()
if remoteName == localName {
fmt.Fprintln(out, localName)
return nil
}
fmt.Fprintf(out, "%s=%s\n", remoteName, localName)
return nil
}); err != nil {
return err
}
fmt.Fprintf(out, "%s=%s\n", remoteName, localName)
return nil
}); err != nil {
return err
}
return nil
},
}
},
}
)

func init() {
bundleDumpCommand.Flags().StringVarP(&bundleDumpFlags.file, "file", "", "", "A file to output")
setup()
bundleDumpFlags.File = defaultFlag.BundleDump.File
bundleDumpCommand.Flags().VarP(&bundleDumpFlags.File, "file", "", "A file to output")
bundleCommand.AddCommand(bundleDumpCommand)
}
Loading

0 comments on commit 03c8f3e

Please sign in to comment.