Skip to content

Commit

Permalink
feat: keep filename
Browse files Browse the repository at this point in the history
  • Loading branch information
Esonhugh committed Sep 19, 2023
1 parent 6e714af commit 27776d5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
13 changes: 8 additions & 5 deletions cmd/management/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import (
)

var AddCmdOpts struct {
FromFile bool
SetEnable bool
UpdateMode bool
Name string
FromFile bool
SetNameAsFileName bool
SetEnable bool
UpdateMode bool
Name string
}

func init() {
AddCmd.Flags().BoolVarP(&AddCmdOpts.FromFile, "from-file", "f", false, "Add From File")
AddCmd.Flags().BoolVarP(&AddCmdOpts.SetEnable, "enable", "e", true, "Set snippet default as Enable")
AddCmd.Flags().BoolVarP(&AddCmdOpts.UpdateMode, "update", "u", false, "Update mode")
AddCmd.Flags().BoolVarP(&AddCmdOpts.SetNameAsFileName, "keep-file-name", "k", false, "Set Name as file Name")

AddCmd.Flags().StringVarP(&AddCmdOpts.Name, "name", "n", "",
"Overwrite Snippet Name (pair with from-file=false), If name is same, add command will update that content")
cmd.RootCmd.AddCommand(AddCmd)
Expand All @@ -36,7 +39,7 @@ var AddCmd = &cobra.Command{
// filename is the name
// content is file data
// Enable set as AddAsSetEnable
cmd_impel.AddFromFiles(args, AddCmdOpts.SetEnable)
cmd_impel.AddFromFiles(args, AddCmdOpts.SetEnable, AddCmdOpts.SetNameAsFileName)
} else {
// Add from Editor
// Name Ask name
Expand Down
12 changes: 10 additions & 2 deletions core/cmd_impel/internal_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ import (
"fmt"
log "github.com/sirupsen/logrus"
"os"
"path/filepath"
"sss/core/defines"
"sss/utils/Ask"
"sss/utils/File"
"sss/utils/TextEditorCall"
"strings"
)

func AddFromFiles(files []string, enable bool) {
func AddFromFiles(files []string, enable bool, keepFileName bool) {
for _, file := range files {
if !File.IsFile(file) {
log.Errorf("file %v not exists or is dir", file)
return
}
var sn defines.ShellSnippet
sn.Name = Ask.ForName(fmt.Sprintf("Input file %v, set snippet name as", file))
if keepFileName {
fileName := filepath.Base(file)
baseName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
sn.Name = baseName
} else {
sn.Name = Ask.ForName(fmt.Sprintf("Input file %v, set snippet name as", file))
}
byteData, err := os.ReadFile(file)
if err != nil {
log.Errorf("Read File %v content error: %v", file, err)
Expand Down
15 changes: 15 additions & 0 deletions core/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core

import (
"path/filepath"
"strings"
"testing"
)

func TestFilename(t *testing.T) {
fn := "/Base/Helloworld.php"
t.Log("Filename :", fn)
fileName := filepath.Base(fn)
baseName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
t.Log(baseName)
}

0 comments on commit 27776d5

Please # to comment.