• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kai20020918 / makit / 15777722451

20 Jun 2025 11:15AM UTC coverage: 72.897% (+66.4%) from 6.522%
15777722451

push

github

kai20020918
fix

78 of 107 relevant lines covered (72.9%)

5.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.9
/cmd/root.go
1
// cmd/root.go
2
package cmd
3

4
import (
5
        "fmt"
6
        "os"
7
        "path/filepath"
8
        "strconv"
9
        "time"
10

11
        "github.com/spf13/cobra"
12
        // pflag をインポートに追加 (もしあれば)
13
)
14

15
var (
16
        mode      string
17
        timestamp string
18
        noCreate  bool
19
        verbose   bool
20
        // exitFunc を追加し、テスト中に os.Exit をモック可能にする
21
        exitFunc = os.Exit // デフォルトは os.Exit
22
)
23

24
// newRootCommand は新しい Cobra コマンドインスタンスを作成し、フラグを初期化します。
25
// これにより、各テストが独立したコマンドインスタンスを持つことができます。
26
func newRootCommand() *cobra.Command {
13✔
27
        cmd := &cobra.Command{
13✔
28
                Use:   "makit [OPTION] <FILES|DIRS...>",
13✔
29
                Short: "Create files and directories with optional mode and timestamp",
13✔
30
                Long:  `makit is a CLI tool to create directories and files with optional permissions, timestamps, and parent creation.`,
13✔
31
                Args:  cobra.MinimumNArgs(1),
13✔
32
                Run: func(cmd *cobra.Command, args []string) {
25✔
33
                        // ★既存のRunロジックはここに含まれます
12✔
34
                        if verbose {
13✔
35
                                fmt.Println("Starting makit operation in verbose mode.")
1✔
36
                                fmt.Printf("Arguments: %v\n", args)
1✔
37
                                fmt.Printf("Mode: %s, Timestamp: %s, NoCreate: %t\n", mode, timestamp, noCreate)
1✔
38
                        }
1✔
39

40
                        var perm os.FileMode = 0755
12✔
41
                        if mode != "" {
14✔
42
                                parsed, err := strconv.ParseUint(mode, 8, 32)
2✔
43
                                if err == nil {
3✔
44
                                        perm = os.FileMode(parsed)
1✔
45
                                } else {
2✔
46
                                        fmt.Fprintf(os.Stderr, "invalid mode: %v\n", err)
1✔
47
                                        exitFunc(1) // ★ os.Exit(1) を exitFunc(1) に変更
1✔
48
                                }
1✔
49
                                if verbose {
1✔
50
                                        fmt.Printf("Parsed permission mode: %o\n", perm)
×
51
                                }
×
52
                        }
53

54
                        var tsTime time.Time
11✔
55
                        if timestamp != "" {
13✔
56
                                t, err := time.Parse("200601021504", timestamp)
2✔
57
                                if err != nil {
3✔
58
                                        fmt.Fprintf(os.Stderr, "invalid timestamp format: %v\n", err)
1✔
59
                                        exitFunc(1) // ★ os.Exit(1) を exitFunc(1) に変更
1✔
60
                                }
1✔
61
                                tsTime = t
1✔
62
                                if verbose {
1✔
63
                                        fmt.Printf("Parsed timestamp: %s\n", tsTime.Format("2006-01-02 15:04:05"))
×
64
                                }
×
65
                        }
66

67
                        for _, path := range args {
22✔
68
                                if verbose {
13✔
69
                                        fmt.Printf("Processing path: %s\n", path)
1✔
70
                                }
1✔
71

72
                                _, err := os.Stat(path)
12✔
73
                                if os.IsNotExist(err) {
23✔
74
                                        if noCreate {
12✔
75
                                                fmt.Printf("Skipped (not created): %s\n", path)
1✔
76
                                                if verbose {
1✔
77
                                                        fmt.Println("Path does not exist and --no-create is set.")
×
78
                                                }
×
79
                                                continue
1✔
80
                                        }
81

82
                                        if filepath.Ext(path) == "" { // ディレクトリと判断
13✔
83
                                                if verbose {
3✔
84
                                                        fmt.Printf("Path '%s' is identified as a directory. Creating...\n", path)
×
85
                                                }
×
86
                                                err := os.MkdirAll(path, perm)
3✔
87
                                                if err != nil {
3✔
88
                                                        fmt.Printf("Error creating directory: %v\n", err)
×
89
                                                        exitFunc(1) // ★ os.Exit(1) を exitFunc(1) に変更
×
90
                                                }
×
91
                                                fmt.Printf("Created directory: %s\n", path)
3✔
92
                                        } else { // ファイルと判断
7✔
93
                                                if verbose {
8✔
94
                                                        fmt.Printf("Path '%s' is identified as a file. Creating...\n", path)
1✔
95
                                                }
1✔
96
                                                dir := filepath.Dir(path)
7✔
97
                                                if dir != "." {
9✔
98
                                                        if verbose {
2✔
99
                                                                fmt.Printf("Ensuring parent directory exists: %s\n", dir)
×
100
                                                        }
×
101
                                                        os.MkdirAll(dir, perm) // 親ディレクトリもデフォルトパーミッションで作成
2✔
102
                                                }
103
                                                f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, perm)
7✔
104
                                                if err != nil {
7✔
105
                                                        fmt.Printf("Error creating file: %v\n", err)
×
106
                                                        exitFunc(1) // ★ os.Exit(1) を exitFunc(1) に変更
×
107
                                                }
×
108
                                                f.Close()
7✔
109
                                                fmt.Printf("Created file: %s\n", path)
7✔
110
                                        }
111
                                } else { // パスが存在する場合
1✔
112
                                        fmt.Printf("Exists: %s\n", path)
1✔
113
                                        if verbose {
1✔
114
                                                fmt.Println("Path already exists. Applying mode/timestamp if specified.")
×
115
                                        }
×
116
                                }
117

118
                                if mode != "" {
12✔
119
                                        if verbose {
1✔
120
                                                fmt.Printf("Applying mode %o to %s\n", perm, path)
×
121
                                        }
×
122
                                        os.Chmod(path, perm)
1✔
123
                                }
124
                                if !tsTime.IsZero() {
12✔
125
                                        if verbose {
1✔
126
                                                fmt.Printf("Applying timestamp %s to %s\n", tsTime.Format("2006-01-02 15:04:05"), path)
×
127
                                        }
×
128
                                        os.Chtimes(path, tsTime, tsTime)
1✔
129
                                }
130
                        }
131
                },
132
        }
133

134
    // ★以下を追加 (テスト時に不要な出力抑制のため)
135
    cmd.SilenceUsage = true
13✔
136
    cmd.SilenceErrors = true
13✔
137

13✔
138
        // ★新しいコマンドインスタンスに対してフラグを登録します
13✔
139
        cmd.Flags().StringVarP(&mode, "mode", "m", "", "Set file/directory mode (e.g. 755)")
13✔
140
        cmd.Flags().StringVarP(&timestamp, "date", "d", "", "Set timestamp (e.g. 202504181200)")
13✔
141
        cmd.Flags().BoolVarP(&noCreate, "no-create", "c", false, "Do not create if not exists")
13✔
142
        cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose output")
13✔
143

13✔
144
        return cmd
13✔
145
}
146

147
func Execute() {
×
148
        // 実際のアプリケーション実行時には、ここで新しいコマンドインスタンスを作成して実行します。
×
149
        cmd := newRootCommand()
×
150
        if err := cmd.Execute(); err != nil {
×
151
                fmt.Println(err)
×
152
                exitFunc(1) // ★ os.Exit(1) を exitFunc(1) に変更
×
153
        }
×
154
}
155

156
// init() 関数はもう不要です(フラグ登録は newRootCommand() に移動したため)
157
// func init() { ... }
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc