-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexport.go
58 lines (52 loc) · 1.39 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package cmd
import (
"fmt"
"path"
"strings"
"github.com/DrakeW/corgi/snippet"
"github.com/spf13/cobra"
)
var exportCmd = &cobra.Command{
Use: "export [title]",
Short: "Export a snippet to json file",
Args: cobra.MaximumNArgs(1),
RunE: export,
}
var outputFile string
var fileType string
func export(cmd *cobra.Command, args []string) error {
conf, snippetsMeta, err := loadConfigAndSnippetsMeta()
if err != nil {
return err
}
// find snippet title
var title string
if len(args) == 0 {
title, err = filterSnippetTitle(conf.FilterCmd, snippetsMeta.GetSnippetTitles())
if err != nil {
return err
}
} else {
title = args[0]
}
// find snippet
s, err := snippetsMeta.FindSnippet(title)
if err != nil {
return err
}
if outputFile == "" {
corgiFileName := path.Base(s.GetFilePath())
if fileType == snippet.EXPORT_TYPE_JSON {
outputFile = fmt.Sprintf("./%s", corgiFileName)
} else {
outputFile = fmt.Sprintf("./%s", strings.Split(corgiFileName, ".")[0])
}
}
err = s.Export(outputFile, fileType)
return err
}
func init() {
exportCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Specify the output path of the snippet")
exportCmd.Flags().StringVarP(&fileType, "type", "t", snippet.EXPORT_TYPE_JSON, fmt.Sprintf("Choose export file type. Allowed values are: \"%s\", \"%s\".", snippet.EXPORT_TYPE_JSON, snippet.EXPORT_TYPE_SHELL))
rootCmd.AddCommand(exportCmd)
}