From 8dbe90ce313ca602ddb2d3a2ec520abc0c4641b5 Mon Sep 17 00:00:00 2001 From: Marcusk19 Date: Wed, 6 Mar 2024 19:09:51 -0500 Subject: [PATCH 1/2] add file walk through path --- cmd/init.go | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 25e65b2..369aced 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -3,6 +3,8 @@ package cmd import ( "fmt" "log" + "os" + "path/filepath" "github.com/spf13/cobra" ) @@ -17,7 +19,40 @@ var initCommand = &cobra.Command { if(len(args) <= 0) { log.Fatal(cmd.OutOrStdout(), "no arguments provided to init") } - // we will do something here, for now just print args[0] - fmt.Fprintf(cmd.OutOrStdout(), args[0]) + + if args[0][len(args[0])-1:] != "/" { + log.Fatal("path needs trailing slash") + } + + var files []string + var acceptedfiles [3] string + acceptedfiles[0] = "nvim" + acceptedfiles[1] = "tmux" + acceptedfiles[2] = "alacritty" + + rootpath := args[0] + err := filepath.Walk(rootpath, func(path string, info os.FileInfo, err error) error { + if err != nil { + log.Fatalf("problem walking path %s", err) + return nil + } + + for _, acceptedfile := range(acceptedfiles) { + if path == args[0] + acceptedfile { + files = append(files, path) + } + } + return nil + }) + + if err != nil { + log.Fatal(err) + } + + fmt.Fprintf(cmd.OutOrStdout(), "binaries installed: \n =======================\n") + for _, file := range(files) { + fmt.Fprintf(cmd.OutOrStdout(), file[len(args[0]):] + "\n" ) + } + }, } From 985fdeb14d856ce7da5327377d0e8ca435e2fc62 Mon Sep 17 00:00:00 2001 From: Marcusk19 Date: Wed, 6 Mar 2024 19:20:42 -0500 Subject: [PATCH 2/2] adding default for init command --- cmd/init.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 369aced..cb41060 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -16,11 +16,15 @@ func init() { var initCommand = &cobra.Command { Use: "init", Run: func(cmd *cobra.Command, args []string) { - if(len(args) <= 0) { - log.Fatal(cmd.OutOrStdout(), "no arguments provided to init") + var rootpath string + if len(args) <= 0 { + fmt.Fprintf(cmd.OutOrStdout(), "no path provided, assuming /usr/bin/\n") + rootpath = "/usr/bin/" + } else { + rootpath = args[0] } - if args[0][len(args[0])-1:] != "/" { + if rootpath[len(rootpath)-1:] != "/" { log.Fatal("path needs trailing slash") } @@ -30,7 +34,6 @@ var initCommand = &cobra.Command { acceptedfiles[1] = "tmux" acceptedfiles[2] = "alacritty" - rootpath := args[0] err := filepath.Walk(rootpath, func(path string, info os.FileInfo, err error) error { if err != nil { log.Fatalf("problem walking path %s", err) @@ -38,7 +41,7 @@ var initCommand = &cobra.Command { } for _, acceptedfile := range(acceptedfiles) { - if path == args[0] + acceptedfile { + if path == rootpath + acceptedfile { files = append(files, path) } } @@ -51,7 +54,7 @@ var initCommand = &cobra.Command { fmt.Fprintf(cmd.OutOrStdout(), "binaries installed: \n =======================\n") for _, file := range(files) { - fmt.Fprintf(cmd.OutOrStdout(), file[len(args[0]):] + "\n" ) + fmt.Fprintf(cmd.OutOrStdout(), file[len(rootpath):] + "\n" ) } },