-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
141 lines (111 loc) · 2.22 KB
/
utils.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/manifoldco/promptui"
)
type Category struct {
name string
path string
}
type Page struct {
name string
path string
}
func Xopen(path string) {
var command string
if runtime.GOOS == "windows" {
command = "start"
open(command, path)
} else if runtime.GOOS == "linux" {
visual := os.Getenv("VISUAL")
editor := os.Getenv("EDITOR")
if visual != "" {
command = visual
open(command, path)
} else if editor != "" {
command = editor
open(command, path)
} else {
command = "/usr/bin/edit"
open(command, path)
}
}
}
func open(cmd, path string) {
c := exec.Command(cmd, path)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Run()
}
func replaceExt(filePath, from, to string) string {
ext := filepath.Ext(filePath)
if len(from) > 0 && ext != from {
return filePath
}
return filePath[:len(filePath)-len(ext)] + to
}
func categoryList() []Category {
// ページ一覧を取得
var path string
var categories []Category
cwd, _ := os.Getwd()
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if file.Name() == ".git" || !file.IsDir() {
continue
}
path = filepath.Join(cwd, file.Name())
categories = append(categories, Category{
name: file.Name(),
path: path,
})
}
return categories
}
func genNewPath(categoryPath, fileName string) string {
return filepath.Join(categoryPath, fileName)
}
func inputFileName() string {
validate := func(input string) error {
return nil
}
prompt := promptui.Prompt{
Label: "ファイル名",
Validate: validate,
}
result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return ""
}
return result
}
func pageList(categoryDir string) []Page {
var pages []Page
cwd, _ := os.Getwd()
files, err := ioutil.ReadDir(categoryDir)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if file.Name() == ".git" || file.Name() == "imgs" || file.IsDir() {
continue
}
filePath := filepath.Join(cwd, categoryDir, file.Name())
pages = append(pages, Page{
name: file.Name(),
path: filePath,
})
}
return pages
}