feat: version / log file handling #7
@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
"path"
|
||||||
|
|
||||||
"d1.hackers.moe/cairde/ui"
|
"d1.hackers.moe/cairde/ui"
|
||||||
|
|
||||||
@ -16,10 +17,24 @@ import (
|
|||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var (
|
||||||
|
// Version is the current version of Cairde (LDFLAGS).
|
||||||
|
Version string
|
||||||
|
|
||||||
|
// Commit is the current git commit of Cairde (LDFLAGS).
|
||||||
|
Commit string
|
||||||
|
|
||||||
|
// help is the command-line help output.
|
||||||
help = `cairde [options]
|
help = `cairde [options]
|
||||||
|
|
||||||
A terminal client for metadata resistant messaging built on the Cwtch protocol.
|
_________ ________ ____ ______
|
||||||
|
/ ____/ | / _/ __ \/ __ \/ ____/
|
||||||
|
/ / / /| | / // /_/ / / / / __/
|
||||||
|
/ /___/ ___ |_/ // _, _/ /_/ / /___
|
||||||
|
\____/_/ |_/___/_/ |_/_____/_____/
|
||||||
|
|
||||||
|
Metadata resistant messaging
|
||||||
|
%s
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-a turn on the Tor ACN immediately
|
-a turn on the Tor ACN immediately
|
||||||
@ -30,6 +45,14 @@ Options:
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if Version == "" {
|
||||||
|
Version = "pre-alpha"
|
||||||
|
}
|
||||||
|
if Commit == "" {
|
||||||
|
Commit = " "
|
||||||
|
}
|
||||||
|
cairdeVersion := fmt.Sprintf("%s commit %s", Version, Commit[:7])
|
||||||
|
|
||||||
var (
|
var (
|
||||||
acnFlag bool
|
acnFlag bool
|
||||||
debugFlag bool
|
debugFlag bool
|
||||||
@ -44,39 +67,53 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if helpFlag {
|
if helpFlag {
|
||||||
fmt.Print(help)
|
fmt.Print(fmt.Sprintf(help, cairdeVersion))
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if versionFlag {
|
if versionFlag {
|
||||||
fmt.Print("cairde pre-alpha v0.1.0") // TODO
|
fmt.Printf("%s\n", cairdeVersion)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE(d1): pending https://git.coopcloud.tech/decentral1se/cairde/issues/1
|
|
||||||
_, err := exec.LookPath("tor")
|
_, err := exec.LookPath("tor")
|
||||||
|
// NOTE(d1): pending https://git.coopcloud.tech/decentral1se/cairde/issues/1
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("could not find 'tor' command, is it installed?")
|
log.Fatal("main: could not find 'tor' command, is it installed?")
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := tea.LogToFile("cairde.log", "debug")
|
user, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("main: unable to determine current user: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
userDir := path.Join(user.HomeDir, "/.cairde/")
|
||||||
|
if err := os.Mkdir(userDir, 0764); err != nil {
|
||||||
|
if !os.IsExist(err) {
|
||||||
|
log.Fatalf("main: unable to create user directory: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cairdeLogPath := path.Join(userDir, "cairde.log")
|
||||||
|
f, err := tea.LogToFile(cairdeLogPath, "debug")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
filelogger, err := openPrivacyLog.NewFile(openPrivacyLog.LevelInfo, "cwtch.log")
|
cwtchLogPath := path.Join(userDir, "cwtch.log")
|
||||||
|
filelogger, err := openPrivacyLog.NewFile(openPrivacyLog.LevelInfo, cwtchLogPath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
openPrivacyLog.SetStd(filelogger)
|
openPrivacyLog.SetStd(filelogger)
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := user.Current()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("unable to determine current user: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
p := tea.NewProgram(
|
p := tea.NewProgram(
|
||||||
ui.NewModel(user.Username, user.HomeDir, debugFlag),
|
ui.NewModel(
|
||||||
|
user.Username,
|
||||||
|
user.HomeDir,
|
||||||
|
cairdeVersion,
|
||||||
|
debugFlag,
|
||||||
|
),
|
||||||
tea.WithAltScreen(),
|
tea.WithAltScreen(),
|
||||||
tea.WithMouseAllMotion(),
|
tea.WithMouseAllMotion(),
|
||||||
)
|
)
|
||||||
|
9
makefile
9
makefile
@ -1,3 +1,7 @@
|
|||||||
|
COMMIT := $(shell git rev-list -1 HEAD)
|
||||||
|
LDFLAGS := "-X 'main.Commit=$(COMMIT)'"
|
||||||
|
DIST_LDFLAGS := $(LDFLAGS)" -s -w"
|
||||||
|
|
||||||
.PHONY: format check clean build run loc test
|
.PHONY: format check clean build run loc test
|
||||||
|
|
||||||
DEFAULT: run
|
DEFAULT: run
|
||||||
@ -13,10 +17,11 @@ ci:
|
|||||||
@golangci-lint run ./...
|
@golangci-lint run ./...
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@find -type f -name "*.log" -exec rm '{}' \;
|
@go clean && \
|
||||||
|
find ~/.cairde -type f -name "*.log" -exec rm '{}' \;
|
||||||
|
|
||||||
build: clean
|
build: clean
|
||||||
@go build -ldflags="-s -w" -v ./cmd/cairde
|
@go build -ldflags=$(DIST_LDFLAGS) ./cmd/cairde
|
||||||
|
|
||||||
run: build
|
run: build
|
||||||
@./cairde
|
@./cairde
|
||||||
|
13
ui/model.go
13
ui/model.go
@ -17,7 +17,7 @@ import (
|
|||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
var (
|
||||||
welcomeMessage = `
|
welcomeMessage = `
|
||||||
_________ ________ ____ ______
|
_________ ________ ____ ______
|
||||||
/ ____/ | / _/ __ \/ __ \/ ____/
|
/ ____/ | / _/ __ \/ __ \/ ____/
|
||||||
@ -26,7 +26,7 @@ const (
|
|||||||
\____/_/ |_/___/_/ |_/_____/_____/
|
\____/_/ |_/___/_/ |_/_____/_____/
|
||||||
|
|
||||||
Metadata resistant messaging
|
Metadata resistant messaging
|
||||||
Pre-alpha v0.1.0
|
%s
|
||||||
|
|
||||||
Run /start for the getting started guide.
|
Run /start for the getting started guide.
|
||||||
Run /help to see all available commands.`
|
Run /help to see all available commands.`
|
||||||
@ -82,12 +82,14 @@ type model struct {
|
|||||||
input textinput.Model
|
input textinput.Model
|
||||||
hiddenInput string
|
hiddenInput string
|
||||||
|
|
||||||
|
version string
|
||||||
|
|
||||||
statusBuffer chan string
|
statusBuffer chan string
|
||||||
|
|
||||||
debug bool
|
debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewModel(username, homeDir string, debug bool) model { // nolint:revive
|
func NewModel(username, homeDir, version string, debug bool) model { // nolint:revive
|
||||||
input := textinput.New()
|
input := textinput.New()
|
||||||
input.Prompt = "> "
|
input.Prompt = "> "
|
||||||
input.PromptStyle = inputPromptStyle
|
input.PromptStyle = inputPromptStyle
|
||||||
@ -107,6 +109,8 @@ func NewModel(username, homeDir string, debug bool) model { // nolint:revive
|
|||||||
|
|
||||||
input: input,
|
input: input,
|
||||||
|
|
||||||
|
version: version,
|
||||||
|
|
||||||
statusBuffer: make(chan string),
|
statusBuffer: make(chan string),
|
||||||
|
|
||||||
showWelcomeMessage: true,
|
showWelcomeMessage: true,
|
||||||
@ -195,8 +199,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if m.showWelcomeMessage {
|
if m.showWelcomeMessage {
|
||||||
|
withVersion := fmt.Sprintf(welcomeMessage, m.version)
|
||||||
cmds = append(cmds, m.sendStatusCmd(
|
cmds = append(cmds, m.sendStatusCmd(
|
||||||
strings.Split(welcomeMessage, "\n")...,
|
strings.Split(withVersion, "\n")...,
|
||||||
))
|
))
|
||||||
m.showWelcomeMessage = false
|
m.showWelcomeMessage = false
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user