From 0cb5ae613875ca01462e2135fb069217e7dd5002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kouakou=20kouam=C3=A9=20yannick?= Date: Sun, 12 May 2024 02:04:21 +0200 Subject: [PATCH] chores: Make the suggestions requested --- cmd/alias.go | 4 +- cmd/delete.go | 4 +- cmd/info.go | 4 +- cmd/init.go | 2 +- cmd/ls.go | 10 +- cmd/reset.go | 2 +- cmd/root.go | 20 ++-- cmd/set.go | 6 +- cmd/status.go | 2 +- pkg/db/db_test.go | 282 +++++++++++++++++++++++---------------------- pkg/indexer.go | 25 ++-- pkg/utils/utils.go | 10 +- 12 files changed, 190 insertions(+), 181 deletions(-) diff --git a/cmd/alias.go b/cmd/alias.go index 1709539..e85ff66 100644 --- a/cmd/alias.go +++ b/cmd/alias.go @@ -10,7 +10,7 @@ import ( ) var ( - ErrBadUsageAliasCmd error = errors.New("bad usage of alias command") + ErrBadUsageAliasCmd = errors.New("bad usage of alias command") ) // aliasCmd represents the alias command @@ -19,7 +19,7 @@ var aliasCmd = &cobra.Command{ Short: "Sets the alias for a project, whose name might be too big", Long: `The idea is instead of having to type a-very-long-project-name-every-time you can alias it to avlpn or something smaller and use that to query pman`, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, args []string) error { if len(args) != 2 { fmt.Println("Usage: pman alias ") return ErrBadUsageAliasCmd diff --git a/cmd/delete.go b/cmd/delete.go index 88c4def..b759380 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -10,14 +10,14 @@ import ( ) var ( - ErrBadUsageDelCmd error = errors.New("bad usage of delete command") + ErrBadUsageDelCmd = errors.New("bad usage of delete command") ) var delCmd = &cobra.Command{ Use: "delete", Short: "Deletes a project from the index database. This does not delete the project from the filesystem", Aliases: []string{"del", "d"}, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, args []string) error { if len(args) != 1 { fmt.Println("Usage : pman delete ") return ErrBadUsageDelCmd diff --git a/cmd/info.go b/cmd/info.go index 2b35730..3c73fa6 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -10,14 +10,14 @@ import ( ) var ( - ErrBadUsageInfoCmd error = errors.New("bad usage of info command") + ErrBadUsageInfoCmd = errors.New("bad usage of info command") ) var infoCmd = &cobra.Command{ Use: "info", Short: "The info command pretty prints the README.md file present at the root of the specified project.", Aliases: []string{"ifo", "ifno", "ino"}, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, args []string) error { if len(args) != 1 { fmt.Println("Please provide a project name") return ErrBadUsageInfoCmd diff --git a/cmd/init.go b/cmd/init.go index b9329f3..da3d2d6 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -15,7 +15,7 @@ var initCmd = &cobra.Command{ Running pman init is the same as running: pman add /* `, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, args []string) error { return pkg.InitDirs(args) }, } diff --git a/cmd/ls.go b/cmd/ls.go index e6f762c..6b3c4f6 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -18,17 +18,19 @@ var lsCmd = &cobra.Command{ `, RunE: func(cmd *cobra.Command, args []string) error { filterFlag, _ := cmd.Flags().GetString("f") + oldUi, _ := cmd.Flags().GetBool("o") data, err := db.GetAllRecords(db.DBName, StatusBucket) if err != nil { return err } if filterFlag != "" { fmt.Println("Filtering by status : ", filterFlag) - data := utils.FilterByStatus(data, filterFlag) - ui.RenderTable(data) + data = utils.FilterByStatus(data, filterFlag) } - ui.RenderTable(data) - return nil + if oldUi { + return ui.RenderTable(data) + } + return ui.RenderInteractiveTable(data) }, } diff --git a/cmd/reset.go b/cmd/reset.go index 852f6f6..1c90373 100644 --- a/cmd/reset.go +++ b/cmd/reset.go @@ -11,7 +11,7 @@ import ( var resetCmd = &cobra.Command{ Use: "reset", Short: "Deletes the current indexed projects, run pman init to reindex the projects", - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, args []string) error { err := db.DeleteDb(db.DBName) if err != nil { fmt.Println(err) diff --git a/cmd/root.go b/cmd/root.go index 97396ee..4889b96 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,20 +7,24 @@ import ( ) const ( - StatusBucket string = "projects" - ProjectPathBucket string = "projectPaths" - ProjectAliasBucket string = "projectAliases" - version string = "1.0" + StatusBucket = "projects" + ProjectPathBucket = "projectPaths" + ProjectAliasBucket = "projectAliases" + version = "1.0" +) + +var ( + ErrNoArgs = errors.New("this command has no argument") ) var rootCmd = &cobra.Command{ - Use: "pman", - Short: "A cli project manager", - Version: version, + Use: "pman", + Short: "A cli project manager", + Version: version, SilenceUsage: true, RunE: func(_ *cobra.Command, args []string) error { if len(args) == 0 { - return errors.New("this command has no argument") + return ErrNoArgs } return nil }, diff --git a/cmd/set.go b/cmd/set.go index 8290656..e72ad48 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -10,8 +10,8 @@ import ( ) var ( - ErrFlagNotImplemented error = errors.New("flag not implemented yet") - ErrBadUsageSetCmd error = errors.New("bad usage of set command") + ErrFlagNotImplemented = errors.New("flag not implemented yet") + ErrBadUsageSetCmd = errors.New("bad usage of set command") ) var setCmd = &cobra.Command{ @@ -26,7 +26,7 @@ var setCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { interactiveFlag, _ := cmd.Flags().GetBool("i") // TODO: Implement this if interactiveFlag { - fmt.Println("Not implemented yet") + cmd.SilenceUsage = true return ErrFlagNotImplemented } if len(args) != 2 { diff --git a/cmd/status.go b/cmd/status.go index 04cebae..2ec84e6 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -19,7 +19,7 @@ var statusCmd = &cobra.Command{ Use: "status", Short: "Get the status of a project", Long: `Query the database for the status of a project.`, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(_ *cobra.Command, args []string) error { var alias string if len(args) != 1 { fmt.Println("Please provide a project name") diff --git a/pkg/db/db_test.go b/pkg/db/db_test.go index 72757f2..7c3319b 100644 --- a/pkg/db/db_test.go +++ b/pkg/db/db_test.go @@ -4,20 +4,30 @@ import ( "os" "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/assert" "github.com/theredditbandit/pman/pkg/db" bolt "go.etcd.io/bbolt" ) +const dbname = db.DBTestName +const bucketName = "testBucket" +const key = "testKey" + func Test_GetDBLoc(t *testing.T) { t.Run("Test getDBLoc", func(t *testing.T) { expectedWords := []string{".local", "share", "pman"} - actualPath, err := db.GetDBLoc(db.DBTestName) - defer os.Remove(actualPath) + actualPath, err := db.GetDBLoc(dbname) + + t.Cleanup(func() { + os.Remove(actualPath) + }) - require.Equal(t, err, nil) - require.Contains(t, actualPath, expectedWords[0], expectedWords[1], expectedWords[2], db.DBTestName) + assert.Equal(t, err, nil) + assert.Contains(t, actualPath, expectedWords[0]) + assert.Contains(t, actualPath, expectedWords[1]) + assert.Contains(t, actualPath, expectedWords[2]) + assert.Contains(t, actualPath, db.DBTestName) }) t.Run("Test GetDBLoc with empty dbname", func(t *testing.T) { @@ -26,9 +36,8 @@ func Test_GetDBLoc(t *testing.T) { actualPath, err := db.GetDBLoc(dbname) - require.Error(t, err) - require.Equal(t, expectedErr, err) - require.Empty(t, actualPath) + assert.ErrorIs(t, err, expectedErr) + assert.Empty(t, actualPath) }) t.Run("Test getDBLoc with panic", func(t *testing.T) { @@ -38,21 +47,21 @@ func Test_GetDBLoc(t *testing.T) { func Test_GetRecord(t *testing.T) { t.Run("Test GetRecord", func(t *testing.T) { - dbname := db.DBTestName - DBLoc, err := db.GetDBLoc(dbname) - require.NoError(t, err) - defer os.Remove(DBLoc) + actualPath, err := db.GetDBLoc(dbname) + assert.NoError(t, err) + + t.Cleanup(func() { + os.Remove(actualPath) + }) expectedValue := "testValue" - key := "testKey" - bucketName := "testBucket" err = db.WriteToDB(dbname, map[string]string{key: expectedValue}, bucketName) - require.NoError(t, err) + assert.NoError(t, err) actualValue, err := db.GetRecord(dbname, key, bucketName) - require.NoError(t, err) - require.Equal(t, expectedValue, actualValue) + assert.NoError(t, err) + assert.Equal(t, expectedValue, actualValue) }) t.Run("Test GetRecord with empty dbname", func(t *testing.T) { @@ -63,49 +72,45 @@ func Test_GetRecord(t *testing.T) { actualValue, err := db.GetRecord(dbname, key, bucketName) - require.Error(t, err) - require.Equal(t, expectedErr, err) - require.Empty(t, actualValue) + assert.ErrorIs(t, err, expectedErr) + assert.Empty(t, actualValue) }) t.Run("Test GetRecord with key not found", func(t *testing.T) { - dbname := db.DBTestName - DBLoc, err := db.GetDBLoc(dbname) - require.NoError(t, err) - defer os.Remove(DBLoc) - key := "testKey" - bucketName := "testBucket" expectedErr := db.ErrKeyNotFound + actualPath, err := db.GetDBLoc(dbname) + assert.NoError(t, err) + + t.Cleanup(func() { + os.Remove(actualPath) + }) err = db.WriteToDB(dbname, map[string]string{}, bucketName) - require.NoError(t, err) + assert.NoError(t, err) actualValue, err := db.GetRecord(dbname, key, bucketName) - require.Error(t, err) - require.Equal(t, expectedErr, err) - require.Empty(t, actualValue) + assert.ErrorIs(t, err, expectedErr) + assert.Empty(t, actualValue) }) t.Run("Test GetRecord with bucket not found", func(t *testing.T) { - dbname := db.DBTestName - key := "testKey" - bucketName := "testBucket" expectedErr := db.ErrBucketNotFound actualValue, err := db.GetRecord(dbname, key, bucketName) - require.Error(t, err) - require.Equal(t, expectedErr, err) - require.Empty(t, actualValue) + assert.ErrorIs(t, err, expectedErr) + assert.Empty(t, actualValue) }) } func Test_WriteToDB(t *testing.T) { t.Run("Test WriteToDB", func(t *testing.T) { - dbname := db.DBTestName - DBLoc, err := db.GetDBLoc(dbname) - require.NoError(t, err) - defer os.Remove(DBLoc) + actualPath, err := db.GetDBLoc(dbname) + assert.NoError(t, err) + + t.Cleanup(func() { + os.Remove(actualPath) + }) data := map[string]string{ "key1": "value1", @@ -115,32 +120,34 @@ func Test_WriteToDB(t *testing.T) { bucketName := "testBucket" err = db.WriteToDB(dbname, data, bucketName) - require.NoError(t, err) + assert.NoError(t, err) // Verify that the data was written correctly - db, err := bolt.Open(DBLoc, 0o600, nil) - require.NoError(t, err) + db, err := bolt.Open(actualPath, 0o600, nil) + assert.NoError(t, err) defer db.Close() err = db.View(func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte(bucketName)) - require.NotNil(t, bucket) + assert.NotNil(t, bucket) for k, v := range data { value := bucket.Get([]byte(k)) - require.Equal(t, []byte(v), value) + assert.Equal(t, []byte(v), value) } return nil }) - require.NoError(t, err) + assert.NoError(t, err) }) t.Run("Test WriteToDB with empty bucketname", func(t *testing.T) { - dbname := db.DBTestName - DBLoc, err := db.GetDBLoc(dbname) - require.NoError(t, err) - defer os.Remove(DBLoc) + actualPath, err := db.GetDBLoc(dbname) + assert.NoError(t, err) + + t.Cleanup(func() { + os.Remove(actualPath) + }) data := map[string]string{ "key1": "value1", @@ -151,25 +158,24 @@ func Test_WriteToDB(t *testing.T) { err = db.WriteToDB(dbname, data, bucketName) - require.Error(t, err) - require.ErrorIs(t, err, db.ErrCreateBucket) + assert.ErrorIs(t, err, db.ErrCreateBucket) }) t.Run("Test WriteToDB with empty map key", func(t *testing.T) { - dbname := db.DBTestName - DBLoc, err := db.GetDBLoc(dbname) - require.NoError(t, err) - defer os.Remove(DBLoc) + actualPath, err := db.GetDBLoc(dbname) + assert.NoError(t, err) + + t.Cleanup(func() { + os.Remove(actualPath) + }) data := map[string]string{ "": "value1", } - bucketName := "testBucket" err = db.WriteToDB(dbname, data, bucketName) - require.Error(t, err) - require.ErrorIs(t, err, db.ErrWriteToDB) + assert.ErrorIs(t, err, db.ErrWriteToDB) }) t.Run("Test WriteToDB with empty dbname value", func(t *testing.T) { @@ -183,150 +189,150 @@ func Test_WriteToDB(t *testing.T) { err := db.WriteToDB(dbname, data, bucketName) - require.Error(t, err) - require.ErrorIs(t, err, db.ErrOpenDB) + assert.ErrorIs(t, err, db.ErrOpenDB) }) } func Test_DeleteFromDb(t *testing.T) { t.Run("Test DeleteFromDb", func(t *testing.T) { - dbname := db.DBTestName - DBLoc, err := db.GetDBLoc(dbname) - require.NoError(t, err) - defer os.Remove(DBLoc) + + actualPath, err := db.GetDBLoc(dbname) + assert.NoError(t, err) + + t.Cleanup(func() { + os.Remove(actualPath) + }) data := map[string]string{ "key1": "value1", "key2": "value2", "key3": "value3", } - bucketName := "testBucket" key := "key1" err = db.WriteToDB(dbname, data, bucketName) - require.NoError(t, err) + assert.NoError(t, err) err = db.DeleteFromDb(dbname, key, bucketName) - require.NoError(t, err) + assert.NoError(t, err) // Verify that the key was deleted - db, err := bolt.Open(DBLoc, 0o600, nil) - require.NoError(t, err) + db, err := bolt.Open(actualPath, 0o600, nil) + assert.NoError(t, err) defer db.Close() err = db.View(func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte(bucketName)) - require.NotNil(t, bucket) + assert.NotNil(t, bucket) value := bucket.Get([]byte(key)) - require.Nil(t, value) + assert.Nil(t, value) return nil }) - require.NoError(t, err) + assert.NoError(t, err) }) t.Run("Test DeleteFromDb with empty dbname", func(t *testing.T) { dbname := "" key := "key1" - bucketName := "testBucket" expectedErr := db.ErrDBNameEmpty err := db.DeleteFromDb(dbname, key, bucketName) - require.Error(t, err) - require.ErrorIs(t, err, expectedErr) + assert.ErrorIs(t, err, expectedErr) }) t.Run("Test DeleteFromDb with key not found", func(t *testing.T) { - dbname := db.DBTestName - DBLoc, err := db.GetDBLoc(dbname) - require.NoError(t, err) - defer os.Remove(DBLoc) + + actualPath, err := db.GetDBLoc(dbname) + assert.NoError(t, err) + + t.Cleanup(func() { + os.Remove(actualPath) + }) data := map[string]string{ "key1": "value1", "key2": "value2", "key3": "value3", } - bucketName := "testBucket" key := "key4" err = db.WriteToDB(dbname, data, bucketName) - require.NoError(t, err) + assert.NoError(t, err) err = db.DeleteFromDb(dbname, key, bucketName) - require.NoError(t, err) + assert.NoError(t, err) }) t.Run("Test DeleteFromDb with bucket not found", func(t *testing.T) { - dbname := db.DBTestName + key := "key1" - bucketName := "testBucket" expectedErr := db.ErrBucketNotFound err := db.DeleteFromDb(dbname, key, bucketName) - require.Error(t, err) - require.ErrorIs(t, err, expectedErr) + assert.ErrorIs(t, err, expectedErr) }) } func Test_ListAllRecords(t *testing.T) { t.Run("Test ListAllRecords", func(t *testing.T) { - dbname := db.DBTestName - DBLoc, err := db.GetDBLoc(dbname) - require.NoError(t, err) - defer os.Remove(DBLoc) + + actualPath, err := db.GetDBLoc(dbname) + assert.NoError(t, err) + + t.Cleanup(func() { + os.Remove(actualPath) + }) data := map[string]string{ "key1": "value1", "key2": "value2", "key3": "value3", } - bucketName := "testBucket" err = db.WriteToDB(dbname, data, bucketName) - require.NoError(t, err) + assert.NoError(t, err) records, err := db.GetAllRecords(dbname, bucketName) - require.NoError(t, err) - require.Equal(t, data, records) + assert.NoError(t, err) + assert.Equal(t, records, data) }) t.Run("Test ListAllRecords with empty dbname", func(t *testing.T) { dbname := "" - bucketName := "testBucket" expectedErr := db.ErrDBNameEmpty expectedValue := map[string]string{} records, err := db.GetAllRecords(dbname, bucketName) - require.Error(t, err) - require.ErrorIs(t, err, expectedErr) - require.Equal(t, records, expectedValue) + assert.ErrorIs(t, err, expectedErr) + assert.Equal(t, records, expectedValue) }) t.Run("Test ListAllRecords with bucket not found", func(t *testing.T) { - dbname := db.DBTestName - bucketName := "testBucket" + expectedErr := db.ErrBucketNotFound records, err := db.GetAllRecords(dbname, bucketName) - require.Error(t, err) - require.ErrorIs(t, err, expectedErr) - require.Nil(t, records) + assert.ErrorIs(t, err, expectedErr) + assert.Nil(t, records) }) } func Test_UpdateRec(t *testing.T) { t.Run("Test UpdateRec", func(t *testing.T) { - dbname := db.DBTestName - DBLoc, err := db.GetDBLoc(dbname) - require.NoError(t, err) - defer os.Remove(DBLoc) + + actualPath, err := db.GetDBLoc(dbname) + assert.NoError(t, err) + + t.Cleanup(func() { + os.Remove(actualPath) + }) data := map[string]string{ "key1": "value1", @@ -338,89 +344,88 @@ func Test_UpdateRec(t *testing.T) { newValue := "updatedValue" err = db.WriteToDB(dbname, data, bucketName) - require.NoError(t, err) + assert.NoError(t, err) err = db.UpdateRec(dbname, key, newValue, bucketName) - require.NoError(t, err) + assert.NoError(t, err) // Verify that the value was updated - db, err := bolt.Open(DBLoc, 0o600, nil) - require.NoError(t, err) + db, err := bolt.Open(actualPath, 0o600, nil) + assert.NoError(t, err) defer db.Close() err = db.View(func(tx *bolt.Tx) error { bucket := tx.Bucket([]byte(bucketName)) - require.NotNil(t, bucket) + assert.NotNil(t, bucket) value := bucket.Get([]byte(key)) - require.Equal(t, []byte(newValue), value) + assert.Equal(t, []byte(newValue), value) return nil }) - require.NoError(t, err) + assert.NoError(t, err) }) t.Run("Test UpdateRec with empty dbname", func(t *testing.T) { dbname := "" key := "key1" newValue := "updatedValue" - bucketName := "testBucket" err := db.UpdateRec(dbname, key, newValue, bucketName) - require.Error(t, err) - require.Equal(t, db.ErrDBNameEmpty, err) + assert.ErrorIs(t, err, db.ErrDBNameEmpty) }) t.Run("Test UpdateRec with key not found", func(t *testing.T) { - dbname := db.DBTestName - DBLoc, err := db.GetDBLoc(dbname) - require.NoError(t, err) - defer os.Remove(DBLoc) + + actualPath, err := db.GetDBLoc(dbname) + assert.NoError(t, err) + + t.Cleanup(func() { + os.Remove(actualPath) + }) data := map[string]string{ "key1": "value1", "key2": "value2", "key3": "value3", } - bucketName := "testBucket" key := "key4" newValue := "updatedValue" err = db.WriteToDB(dbname, data, bucketName) - require.NoError(t, err) + assert.NoError(t, err) err = db.UpdateRec(dbname, key, newValue, bucketName) - require.Error(t, err) - require.Equal(t, db.ErrProjectNotFound, err) + assert.ErrorIs(t, err, db.ErrProjectNotFound) }) t.Run("Test UpdateRec with bucket not found", func(t *testing.T) { - dbname := db.DBTestName + key := "key1" newValue := "updatedValue" - bucketName := "testBucket" expectedErr := db.ErrBucketNotFound err := db.UpdateRec(dbname, key, newValue, bucketName) - require.Error(t, err) - require.Equal(t, expectedErr, err) + assert.ErrorIs(t, err, expectedErr) }) } func Test_DeleteDb(t *testing.T) { t.Run("Test DeleteDb", func(t *testing.T) { - dbname := db.DBTestName - DBLoc, err := db.GetDBLoc(dbname) - require.NoError(t, err) - defer os.Remove(DBLoc) + actualPath, err := db.GetDBLoc(dbname) + assert.NoError(t, err) + + t.Cleanup(func() { + os.Remove(actualPath) + }) err = db.DeleteDb(dbname) - require.NoError(t, err) + assert.NoError(t, err) // Verify that the database file is deleted - _, err = os.Stat(DBLoc) - require.True(t, os.IsNotExist(err)) + _, err = os.Stat(actualPath) + assert.True(t, os.IsNotExist(err)) }) t.Run("Test DeleteDb with empty dbname", func(t *testing.T) { @@ -429,7 +434,6 @@ func Test_DeleteDb(t *testing.T) { err := db.DeleteDb(dbname) - require.Error(t, err) - require.Equal(t, expectedErr, err) + assert.ErrorIs(t, err, expectedErr) }) } diff --git a/pkg/indexer.go b/pkg/indexer.go index 6a5e69e..dff5575 100644 --- a/pkg/indexer.go +++ b/pkg/indexer.go @@ -2,7 +2,6 @@ package pkg import ( "errors" - "fmt" "log" "os" "path/filepath" @@ -11,16 +10,16 @@ import ( ) const ( - StatusBucket string = "projects" - ProjectPaths string = "projectPaths" - ProjectAliasBucket string = "projectAliases" + StatusBucket = "projects" + ProjectPaths = "projectPaths" + ProjectAliasBucket = "projectAliases" ) var ( - ErrDirname error = errors.New("error providing a directory name") - ErrDirInvalid error = errors.New("error providing a valid directory name") - ErrIsNotDir error = errors.New("error providing a file instead of a directory") - ErrIndexDir error = errors.New("error indexing directory") + ErrDirname = errors.New("error providing a directory name") + ErrDirInvalid = errors.New("error providing a valid directory name") + ErrIsNotDir = errors.New("error providing a file instead of a directory") + ErrIndexDir = errors.New("error indexing directory") ) // InitDirs indexes a directory for project directories and writes the data to the DB @@ -28,23 +27,23 @@ func InitDirs(args []string) error { // the file which identifies a project directory projIdentifier := "README.md" if len(args) != 1 { - fmt.Println("Please provide a directory name") + log.Print("Please provide a directory name") return ErrDirname } dirname := args[0] if stat, err := os.Stat(dirname); os.IsNotExist(err) { - fmt.Printf("%s is not a directory \n", dirname) + log.Printf("%s is not a directory \n", dirname) return ErrDirInvalid } else if !stat.IsDir() { - fmt.Printf("%s is a file and not a directory \n", dirname) + log.Printf("%s is a file and not a directory \n", dirname) return ErrIsNotDir } projDirs, err := indexDir(dirname, projIdentifier) if err != nil { - fmt.Println(err) + log.Print(err) return ErrIndexDir } - fmt.Printf("Indexed %d project directories . . .\n", len(projDirs)) + log.Printf("Indexed %d project directories . . .\n", len(projDirs)) projectStatusMap := make(map[string]string) projectPathMap := make(map[string]string) for k, v := range projDirs { // k : full project path, v : project status , diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index e69c06e..ddca814 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -17,8 +17,8 @@ import ( ) var ( - ErrBeautifyMD error = errors.New("error beautifying markdown") - ErrReadREADME error = errors.New("error reading README") + ErrBeautifyMD = errors.New("error beautifying markdown") + ErrReadREADME = errors.New("error reading README") ) func TitleCase(s string) string { @@ -41,9 +41,9 @@ func PrintData(data map[string]string) { for k, v := range data { alias, err := db.GetRecord(db.DBName, k, pkg.ProjectAliasBucket) if err == nil { - fmt.Printf("%s : %s (%s) \n", TitleCase(v), k, alias) + log.Printf("%s : %s (%s) \n", TitleCase(v), k, alias) } else { - fmt.Printf("%s : %s \n", TitleCase(v), k) + log.Printf("%s : %s \n", TitleCase(v), k) } } } @@ -108,7 +108,7 @@ func ReadREADME(projectName string) ([]byte, error) { pPath := filepath.Join(path, "README.md") data, err := os.ReadFile(pPath) if err != nil { - return nil, fmt.Errorf("Something went wrong while reading README for %s: %w", projectName, err) + return nil, fmt.Errorf("something went wrong while reading README for %s: %w", projectName, err) } return data, nil }