Skip to content

Commit

Permalink
implement "delete" method (#52)
Browse files Browse the repository at this point in the history
* wip: implement "delete" method

* fix lints
  • Loading branch information
kyoh86 authored Jan 13, 2020
1 parent e2ce2d2 commit bfc2c05
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
45 changes: 45 additions & 0 deletions command/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package command

import (
"fmt"
"os"

"github.com/kyoh86/ask"
"github.com/kyoh86/gogh/gogh"
)

// Delete local projects
func Delete(ctx gogh.Context, primary bool, query string) error {
var walk gogh.Walker = gogh.Walk
if primary {
walk = gogh.WalkInPrimary
}

var projects []*gogh.Project
if err := gogh.Query(ctx, query, walk, func(p *gogh.Project) error {
projects = append(projects, p)
return nil
}); err != nil {
return err
}

if len(projects) == 0 {
fmt.Println("Any projects did not matched for", query)
return nil
}
fmt.Println("Deleting projects. Please confirm them and answer by [y/n]")

for _, p := range projects {
fmt.Print(p.FullPath)
yes, err := ask.YesNo()
if err != nil {
return err
}
if *yes {
if err := os.RemoveAll(p.FullPath); err != nil {
return err
}
}
}
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/karrick/godirwalk v1.13.4
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/kyoh86/ask v0.0.1
github.com/kyoh86/xdg v1.0.0
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.11 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kyoh86/ask v0.0.1 h1:uhacMP1dCqi4fycnnm0kKoiw9cBIGN+7BJ6GiXzTwTk=
github.com/kyoh86/ask v0.0.1/go.mod h1:ziIzeLJ0sSw17NbtLE6bCYS9sEIlEtX5LQ7dPyyiD10=
github.com/kyoh86/xdg v1.0.0 h1:TD1layQ0epNApNwGRblnQnT3S/2UH/gCQN1cmXWotvE=
github.com/kyoh86/xdg v1.0.0/go.mod h1:Z5mDqe0fxyxn3W2yTxsBAOQqIrXADQIh02wrTnaRM38=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
Expand All @@ -67,6 +69,8 @@ github.com/thoas/go-funk v0.5.0/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xz
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac h1:7d7lG9fHOLdL6jZPtnV4LpI41SbohIJ1Atq7U991dMg=
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191219195013-becbf705a915 h1:aJ0ex187qoXrJHPo8ZasVTASQB7llQP6YeNzgDALPRk=
golang.org/x/crypto v0.0.0-20191219195013-becbf705a915/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down Expand Up @@ -104,5 +108,6 @@ google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCID
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func main() {
list,
dump,
find,
remove,
root,
setup,

Expand Down Expand Up @@ -262,6 +263,19 @@ func dump(app *kingpin.Application) (string, func() error) {
})
}

func remove(app *kingpin.Application) (string, func() error) {
var (
primary bool
query string
)
cmd := app.Command("delete", "Delete projects").Alias("remove").Alias("rm")
cmd.Flag("primary", "Only in primary root directory").Short('p').BoolVar(&primary)
cmd.Arg("query", "Project name query").StringVar(&query)

return mainutil.WrapCommand(cmd, func(ctx gogh.Context) error {
return command.Delete(ctx, primary, query)
})
}
func find(app *kingpin.Application) (string, func() error) {
var (
primary bool
Expand Down

0 comments on commit bfc2c05

Please sign in to comment.