Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
a3202aaff3
commit
948872b933
7 changed files with 206 additions and 150 deletions
60
internal/zet/create.go
Normal file
60
internal/zet/create.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package zet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/config"
|
||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/file"
|
||||
)
|
||||
|
||||
func CreateZet(title string) {
|
||||
zid := newZid()
|
||||
|
||||
path := path.Join(config.GetZetDir(), strconv.Itoa(zid))
|
||||
|
||||
os.Mkdir(path, 0750)
|
||||
|
||||
filePath := fmt.Sprintf("%s/index.adoc", path)
|
||||
|
||||
f, err := os.Create(filePath)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
_, err = fmt.Fprintf(f, "= %s", title)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
file.Edit(filePath)
|
||||
|
||||
onSave(zid)
|
||||
}
|
||||
|
||||
func newZid() int {
|
||||
cmd := exec.Command("ls", config.GetZetDir())
|
||||
output, _ := cmd.CombinedOutput()
|
||||
|
||||
zets := strings.Split(string(output), "\n")
|
||||
|
||||
var zetCount int
|
||||
|
||||
for _, zet := range zets {
|
||||
num, err := strconv.Atoi(zet)
|
||||
if err == nil && num > zetCount {
|
||||
zetCount = num
|
||||
}
|
||||
}
|
||||
|
||||
return zetCount + 1
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue