-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
109 lines (98 loc) · 2.32 KB
/
Copy pathapp.go
File metadata and controls
109 lines (98 loc) · 2.32 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package unbox
import (
"errors"
"fmt"
"os"
"path"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{Use: "app"}
func init() {
/*
var cmd = &cobra.Command{
Use: "hello",
Short: "hello",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires at least one arg")
}
// return fmt.Errorf("invalid color specified: %s", args[0])
fmt.Println("test")
return nil
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hello, World!")
},
}
cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.AddCommand(cmd)
*/
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(unpackCmd)
unpackCmd.Flags().StringVarP(&AtlasType, "type", "t", "json", "atlas type")
unpackCmd.Flags().StringVarP(&OutPath, "out", "o", "./out", "out path")
}
var AtlasType string
var OutPath string
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Unbox",
Long: `Print the version number of Unbox`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Unbox 0.1")
},
}
var unpackCmd = &cobra.Command{
Use: "atlas",
Short: "upack atlas",
Long: `unpack atlas`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires at least one arg")
}
if _, err := os.Stat(OutPath); os.IsNotExist(err) {
err = os.MkdirAll(OutPath, 0777)
if err != nil {
return err
}
}
if AtlasType != "json" {
return errors.New("other type come soon")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
var inputPath = args[0]
fi, err := os.Stat(inputPath)
if err != nil {
fmt.Printf("err:%v \n", err)
return
}
ext := "." + AtlasType
switch mode := fi.Mode(); {
case mode.IsDir():
fmt.Println("directory")
err := UnpackDir(inputPath, OutPath, ext)
if err != nil {
fmt.Printf("err:%v \n", err)
}
case mode.IsRegular():
// fmt.Printf("file:%v,%v\n", path.Ext(inputPath), AtlasType)
if path.Ext(inputPath) != ext {
fmt.Printf("ext err \n")
return
}
err := UnpackFile(inputPath, OutPath)
if err != nil {
fmt.Printf("err:%v \n", err)
}
fmt.Println("unpack success")
}
},
}