Skip to content
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ install:
- go install github.com/golangci/golangci-lint/cmd/golangci-lint
- go install github.com/mattn/goveralls

before_script: make lint
before_script: make check-license lint

script:
- make test coverage
Expand Down
48 changes: 23 additions & 25 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,11 @@ __git-ghost_custom_func() {
`
)

func init() {
RootCmd.AddCommand(completionCmd)
}

var completionCmd = &cobra.Command{
Use: "completion SHELL",
Short: "output shell completion code for the specified shell (bash or zsh)",
Long: `Write bash or zsh shell completion code to standard output.
func NewCompletionCommand(rootCmd *cobra.Command) *cobra.Command {
return &cobra.Command{
Use: "completion SHELL",
Short: "output shell completion code for the specified shell (bash or zsh)",
Long: `Write bash or zsh shell completion code to standard output.

For bash, ensure you have bash completions installed and enabled.
To access completions in your current shell, run
Expand All @@ -75,21 +72,22 @@ var completionCmd = &cobra.Command{
For zsh, output to a file in a directory referenced by the $fpath shell
variable.
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
shell := args[0]
RootCmd.BashCompletionFunction = bashCompletionFunc
availableCompletions := map[string]func(io.Writer) error{
"bash": RootCmd.GenBashCompletion,
"zsh": RootCmd.GenZshCompletion,
}
completion, ok := availableCompletions[shell]
if !ok {
fmt.Printf("Invalid shell '%s'. The supported shells are bash and zsh.\n", shell)
os.Exit(1)
}
if err := completion(os.Stdout); err != nil {
log.Fatal(err)
}
},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
shell := args[0]
rootCmd.BashCompletionFunction = bashCompletionFunc
availableCompletions := map[string]func(io.Writer) error{
"bash": rootCmd.GenBashCompletion,
"zsh": rootCmd.GenZshCompletion,
}
completion, ok := availableCompletions[shell]
if !ok {
fmt.Printf("Invalid shell '%s'. The supported shells are bash and zsh.\n", shell)
os.Exit(1)
}
if err := completion(os.Stdout); err != nil {
log.Fatal(err)
}
},
}
}
4 changes: 0 additions & 4 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ import (
"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(NewDeleteCommand())
}

type deleteFlags struct {
hashFrom string
hashTo string
Expand Down
20 changes: 9 additions & 11 deletions cmd/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ import (
"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(gcCmd)
}

var gcCmd = &cobra.Command{
Use: "gc",
Short: "gc ghost commits from remote repository.",
Long: "gc ghost commits from remote repository.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("gc command")
},
func NewGCCommand() *cobra.Command {
return &cobra.Command{
Use: "gc",
Short: "gc ghost commits from remote repository.",
Long: "gc ghost commits from remote repository.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("gc command")
},
}
}
4 changes: 0 additions & 4 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import (
"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(NewListCommand())
}

var outputTypes = []string{"only-from", "only-to"}
var regexpOutputPattern = regexp.MustCompile("^(|" + strings.Join(outputTypes, "|") + ")$")

Expand Down
4 changes: 0 additions & 4 deletions cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ import (
"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(NewPullCommand())
}

type pullFlags struct {
// forceApply bool
}
Expand Down
4 changes: 0 additions & 4 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ type pushFlags struct {
followSymlinks bool
}

func init() {
RootCmd.AddCommand(NewPushCommand())
}

func NewPushCommand() *cobra.Command {
var (
flags pushFlags
Expand Down
105 changes: 57 additions & 48 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,59 +55,68 @@ var (
Revision string
)

var RootCmd = &cobra.Command{
Use: "git-ghost",
Short: "git-ghost",
SilenceErrors: false,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if cmd.Use == "version" {
return nil
}
err := validateEnvironment()
if err != nil {
return err
}
err = globalOpts.SetDefaults()
if err != nil {
return err
}
err = globalOpts.Validate()
if err != nil {
return err
}
switch globalOpts.verbose {
case 0:
log.SetLevel(log.ErrorLevel)
case 1:
log.SetLevel(log.InfoLevel)
case 2:
log.SetLevel(log.DebugLevel)
case 3:
log.SetLevel(log.TraceLevel)
}
return nil
},
}

var globalOpts globalFlags

func init() {
func NewRootCmd() *cobra.Command {
cobra.OnInitialize()
RootCmd.PersistentFlags().StringVar(&globalOpts.srcDir, "src-dir", "", "source directory which you create ghost from (default to PWD env)")
RootCmd.PersistentFlags().StringVar(&globalOpts.ghostWorkDir, "ghost-working-dir", "", "local root directory for git-ghost interacting with ghost repository (default to a temporary directory)")
RootCmd.PersistentFlags().StringVar(&globalOpts.ghostPrefix, "ghost-prefix", "", "prefix of ghost branch name (default to GIT_GHOST_PREFIX env, or ghost)")
RootCmd.PersistentFlags().StringVar(&globalOpts.ghostRepo, "ghost-repo", "", "git remote url for ghosts repository (default to GIT_GHOST_REPO env)")
RootCmd.PersistentFlags().CountVarP(&globalOpts.verbose, "verbose", "v", "verbose mode")
RootCmd.AddCommand(versionCmd)
rootCmd := &cobra.Command{
Use: "git-ghost",
Short: "git-ghost",
SilenceErrors: false,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if cmd.Use == "version" {
return nil
}
err := validateEnvironment()
if err != nil {
return err
}
err = globalOpts.SetDefaults()
if err != nil {
return err
}
err = globalOpts.Validate()
if err != nil {
return err
}
switch globalOpts.verbose {
case 0:
log.SetLevel(log.ErrorLevel)
case 1:
log.SetLevel(log.InfoLevel)
case 2:
log.SetLevel(log.DebugLevel)
case 3:
log.SetLevel(log.TraceLevel)
}
return nil
},
}
rootCmd.PersistentFlags().StringVar(&globalOpts.srcDir, "src-dir", "", "source directory which you create ghost from (default to PWD env)")
rootCmd.PersistentFlags().StringVar(&globalOpts.ghostWorkDir, "ghost-working-dir", "", "local root directory for git-ghost interacting with ghost repository (default to a temporary directory)")
rootCmd.PersistentFlags().StringVar(&globalOpts.ghostPrefix, "ghost-prefix", "", "prefix of ghost branch name (default to GIT_GHOST_PREFIX env, or ghost)")
rootCmd.PersistentFlags().StringVar(&globalOpts.ghostRepo, "ghost-repo", "", "git remote url for ghosts repository (default to GIT_GHOST_REPO env)")
rootCmd.PersistentFlags().CountVarP(&globalOpts.verbose, "verbose", "v", "verbose mode")
rootCmd.AddCommand(NewPushCommand())
rootCmd.AddCommand(NewPullCommand())
rootCmd.AddCommand(NewShowCommand())
rootCmd.AddCommand(NewListCommand())
rootCmd.AddCommand(NewDeleteCommand())
rootCmd.AddCommand(NewGCCommand())
rootCmd.AddCommand(NewVersionCommand())
rootCmd.AddCommand(NewCompletionCommand(rootCmd))
return rootCmd
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of git-ghost",
Long: `Print the version number of git-ghost`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("git-ghost %s (revision: %s)", Version, Revision)
},
func NewVersionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version number of git-ghost",
Long: `Print the version number of git-ghost`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("git-ghost %s (revision: %s)", Version, Revision)
},
}
}

func validateEnvironment() errors.GitGhostError {
Expand Down
26 changes: 26 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2019 Preferred Networks, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRootCmd(t *testing.T) {
cmd := NewRootCmd()
assert.NotNil(t, cmd)
}
4 changes: 0 additions & 4 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ import (
"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(NewShowCommand())
}

func NewShowCommand() *cobra.Command {
command := &cobra.Command{
Use: "show [from-hash(default=HEAD)] [diff-hash]",
Expand Down
2 changes: 0 additions & 2 deletions golangci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
run:
tests: false
skip-files:
- "pkg/.*_k8s.go$"
output:
format: colored-line-number
print-issued-lines: true
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ func init() {
}

func main() {
// RootCmd prints errors if exists
if err := cmd.RootCmd.Execute(); err != nil {
// rootCmd prints errors if exists
rootCmd := cmd.NewRootCmd()
if err := rootCmd.Execute(); err != nil {
os.Exit(-1)
}
}
16 changes: 10 additions & 6 deletions scripts/license/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@


def main(verbose=False):
add(PROJECT_ROOT.glob("*[!vendor]/**/*_k8s.go"),
license_header("//", True), verbose)
add([p for p in PROJECT_ROOT.glob("*[!vendor]/**/*.go")
if p.name[-7:] != "_k8s.go"], license_header("//"), verbose)
add(PROJECT_ROOT.glob("*[!vendor]/**/*.py"), license_header("#"), verbose)
add(PROJECT_ROOT.glob("*[!vendor]/**/*.sh"), license_header("#"), verbose)
add(PROJECT_ROOT.glob("*.go"), license_header("//"), verbose)
add(PROJECT_ROOT.glob("*.py"), license_header("#"), verbose)
add(PROJECT_ROOT.glob("*.sh"), license_header("#"), verbose)

for p in PROJECT_ROOT.glob("*"):
if p.name == "vendor" or not p.is_dir():
continue
add(p.glob("**/*.go"), license_header("//"), verbose)
add(p.glob("**/*.py"), license_header("#"), verbose)
add(p.glob("**/*.sh"), license_header("#"), verbose)


def add(paths, license_header, verbose):
Expand Down
26 changes: 14 additions & 12 deletions scripts/license/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
Checks whether files have an appropriate license header.
"""

from pathlib import Path
import subprocess
from pathlib import Path

from license_header import license_header, has_license_header

Expand All @@ -28,17 +28,19 @@
def main(verbose=False):
ok = True

ok &= check(
PROJECT_ROOT.glob("./*[!vendor]/**/*_k8s.go"),
license_header("//", modification=True), verbose)
ok &= check(
[p for p in PROJECT_ROOT.glob(
"./*[!vendor]/**/*.go") if p.name[-7:] != "_k8s.go"],
license_header("//"), verbose)
ok &= check(
PROJECT_ROOT.glob("./*[!vendor]/**/*.py"), license_header("#"), verbose)
ok &= check(
PROJECT_ROOT.glob("./*[!vendor]/**/*.sh"), license_header("#"), verbose)
ok &= check(PROJECT_ROOT.glob("*.go"), license_header("//"), verbose)
ok &= check(PROJECT_ROOT.glob("*.py"), license_header("#"), verbose)
ok &= check(PROJECT_ROOT.glob("*.sh"), license_header("#"), verbose)

for p in PROJECT_ROOT.glob("*"):
if p.name == "vendor" or not p.is_dir():
continue
ok &= check(
p.glob("**/*.go"), license_header("//"), verbose)
ok &= check(
p.glob("**/*.py"), license_header("#"), verbose)
ok &= check(
p.glob("**/*.sh"), license_header("#"), verbose)

return 0 if ok else 1

Expand Down