From 37619b17cedebac3e68a37f90acbe13aa2fd80e0 Mon Sep 17 00:00:00 2001 From: Mya Pitzeruse Date: Tue, 1 Jan 2019 14:01:57 -0600 Subject: [PATCH] gh-14: added stop command (#22) * gh-14: added stop command * gh-14: fixed up imports --- cmd/gitfs/main.go | 4 +++- cmd/global.go | 3 ++- cmd/start.go | 9 +++++---- cmd/stop.go | 34 ++++++++++++++++++++++++++++++++++ go.mod | 2 +- pkg/clone/cloner.go | 5 +++-- 6 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 cmd/stop.go diff --git a/cmd/gitfs/main.go b/cmd/gitfs/main.go index aa2ecdf..4986c1a 100644 --- a/cmd/gitfs/main.go +++ b/cmd/gitfs/main.go @@ -2,9 +2,10 @@ package main import ( "fmt" + "os" + "github.com/mjpitz/gitfs/cmd" "github.com/spf13/cobra" - "os" ) func main() { @@ -18,6 +19,7 @@ func main() { "Specify the configuration path") rootCmd.AddCommand(cmd.StartCommand) + rootCmd.AddCommand(cmd.StopCommand) if err := rootCmd.Execute(); err != nil { fmt.Println(err) diff --git a/cmd/global.go b/cmd/global.go index d837dbe..326f6f5 100644 --- a/cmd/global.go +++ b/cmd/global.go @@ -1,8 +1,9 @@ package cmd import ( - "github.com/sirupsen/logrus" "os" + + "github.com/sirupsen/logrus" ) var ( diff --git a/cmd/start.go b/cmd/start.go index ea0a55e..e30f9ec 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -1,6 +1,11 @@ package cmd import ( + "os" + "os/user" + "strconv" + "strings" + "bazil.org/fuse" "bazil.org/fuse/fs" "github.com/mjpitz/gitfs/pkg/clone" @@ -10,10 +15,6 @@ import ( "github.com/mjpitz/gitfs/pkg/tree" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "os" - "os/user" - "strconv" - "strings" ) var StartCommand = &cobra.Command{ diff --git a/cmd/stop.go b/cmd/stop.go new file mode 100644 index 0000000..eb7996c --- /dev/null +++ b/cmd/stop.go @@ -0,0 +1,34 @@ +package cmd + +import ( + "os" + "syscall" + + "github.com/mjpitz/gitfs/pkg/config" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "golang.org/x/sys/unix" +) + +var StopCommand = &cobra.Command{ + Use: "stop", + Short: "Stops the file system server.", + Run: func(cmd *cobra.Command, args []string) { + props := ConfigPath + if len(props) == 0 { + fail("missing config file") + } + + cfg, err := config.Load(os.ExpandEnv(props)) + if err != nil { + fail("failed to parse configuration: %v", err) + } + + mountpoint := os.ExpandEnv(cfg.Mount) + + logrus.Infof("Unmounting %s", mountpoint) + + _ = syscall.Unmount(mountpoint, unix.MNT_FORCE) + // ignore the error since it's likely not mounted + }, +} diff --git a/go.mod b/go.mod index affb763..a06215d 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( golang.org/x/net v0.0.0-20181220203305-927f97764cc3 golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890 golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 // indirect - golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb // indirect + golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb google.golang.org/appengine v1.4.0 // indirect gopkg.in/src-d/go-billy.v4 v4.3.0 gopkg.in/src-d/go-git-fixtures.v3 v3.3.0 // indirect diff --git a/pkg/clone/cloner.go b/pkg/clone/cloner.go index c7ca9c6..cbbf073 100644 --- a/pkg/clone/cloner.go +++ b/pkg/clone/cloner.go @@ -3,6 +3,9 @@ package clone import ( "crypto/sha256" "encoding/base32" + "os" + "regexp" + "github.com/mjpitz/gitfs/pkg/config" "github.com/mjpitz/gitfs/pkg/sync" "github.com/pkg/errors" @@ -13,8 +16,6 @@ import ( "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing/cache" "gopkg.in/src-d/go-git.v4/storage/filesystem" - "os" - "regexp" ) func NewCloner(cfg *config.CloneConfiguration) *Cloner {