Skip to content

Commit 02e4e38

Browse files
authored
Merge pull request #7 from orenoi/pr
🛠️ Restructure CLI, implement version handling, and add install script
2 parents 40f82e0 + 53f5fc4 commit 02e4e38

File tree

14 files changed

+227
-79
lines changed

14 files changed

+227
-79
lines changed

Makefile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
BINARY := pulsarship
22
BUILD_DIR := build
3-
VERSION := $(shell git describe --tags --always --dirty)
4-
#LDFLAGS := -s -w -X main.version=$(VERSION)
3+
VERSION := $(shell git describe --tags --abbrev=0)
4+
TAG := $(shell git describe --tags --abbrev=0)
5+
COMMIT := $(shell git rev-parse --short HEAD)
6+
BUILDTIME := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
7+
BUILDENV := $(shell go version)
8+
LDFLAGS := -X 'main.version=$(VERSION)' -X 'main.tag=$(TAG)' -X 'main.commit=$(COMMIT)' -X 'main.buildTime=$(BUILDTIME)' -X 'main.buildEnv=$(BUILDENV)'
59

610
GOCMD := go
711
GOBUILD := $(GOCMD) build
@@ -15,8 +19,7 @@ all: fmt vet test build install
1519

1620
.PHONY: build
1721
build:
18-
# $(GOBUILD) -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY) .
19-
$(GOBUILD) -o $(BUILD_DIR)/$(BINARY) .
22+
$(GOBUILD) -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY) .
2023

2124
.PHONY: install
2225
install: build

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ or
5454
paru -S pulsarship
5555
```
5656

57-
### 🛠️ Manual Installation
57+
### 🛠️ Install via Script
58+
You can install `pulsarship` with a single command:
59+
5860
```bash
59-
git clone https://github.com/xeyossr/pulsarship
60-
cd pulsarship
61-
make install
61+
curl -sS https://raw.githubusercontent.com/xeyossr/pulsarship/main/install.sh | bash
6262
```
6363

64+
This script will clone the repository, build the binary, and install it for you.
65+
6466
### 🔧 Add to your shell config
6567
Add the following to your `~/.config/fish/config.fish`:
6668
**Fish:**

aur/.SRCINFO

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
pkgbase = pulsarship
22
pkgdesc = 🚀🌠 A minimal, fast, and customizable prompt written in Go
3-
pkgver = 0.2.1
3+
pkgver = 0.2.2
44
pkgrel = 1
55
url = https://github.com/xeyossr/pulsarship
66
arch = x86_64
7+
arch = aarch64
8+
arch = armv7h
9+
arch = i686
710
license = GPL3
811
makedepends = go
912
makedepends = git
10-
source = git+https://github.com/xeyossr/pulsarship.git#tag=v0.2.1
13+
source = git+https://github.com/xeyossr/pulsarship.git#tag=v0.2.2
1114
sha256sums = SKIP
1215

1316
pkgname = pulsarship

aur/PKGBUILD

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Maintainer: kamisaki
22
# Description: 🚀🌠 A minimal, fast, and customizable prompt written in Go
33
pkgname=pulsarship
4-
pkgver=0.2.1
4+
pkgver=0.2.2
55
pkgrel=1
66
pkgdesc="🚀🌠 A minimal, fast, and customizable prompt written in Go"
7-
arch=('x86_64')
7+
arch=('x86_64' 'aarch64' 'armv7h' 'i686')
88
url="https://github.com/xeyossr/pulsarship"
99
license=('GPL3')
1010
depends=()
@@ -17,11 +17,22 @@ sha256sums=('SKIP')
1717

1818
build() {
1919
cd "$srcdir/${pkgname}"
20+
21+
# Fetch version, tag, commit, build time, and build environment dynamically
22+
TAG=$(git describe --tags)
23+
COMMIT=$(git rev-parse --short HEAD)
24+
BUILDTIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)
25+
BUILDENV=$(go version)
26+
27+
LDFLAGS="-X main.version=${pkgver} -X main.tag=${TAG} -X main.commit=${COMMIT} -X main.buildTime=${BUILDTIME} -X main.buildEnv=${BUILDENV}"
28+
29+
# Build the binary with dynamic LDFLAGS
2030
go mod tidy
21-
go build -o pulsarship
31+
go build -ldflags "${LDFLAGS}" -o pulsarship
2232
}
2333

2434
package() {
2535
cd "$srcdir/${pkgname}"
36+
# Install the binary to /usr/bin/
2637
install -Dm755 "${srcdir}/${pkgname}/pulsarship" "${pkgdir}/usr/bin/${pkgname}"
2738
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/xeyossr/pulsarship
33
go 1.24.5
44

55
require (
6-
github.com/BurntSushi/toml v1.5.0
6+
github.com/pelletier/go-toml/v2 v2.2.4
77
github.com/spf13/cobra v1.9.1
88
)
99

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
2-
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
31
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
42
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
53
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
4+
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
5+
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
66
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
77
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
88
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=

install.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/bash
2+
3+
# Pulsarship Installation Script
4+
5+
# Function to print error messages with red color
6+
function print_error {
7+
echo -e "\033[1;31m❌ ERROR: $1\033[0m"
8+
}
9+
10+
# Function to print success messages with green color
11+
function print_success {
12+
echo -e "\033[1;32m✅ SUCCESS: $1\033[0m"
13+
}
14+
15+
# Function to print info messages with blue color
16+
function print_info {
17+
echo -e "\033[1;34mℹ️ INFO: $1\033[0m"
18+
}
19+
20+
# Function to print warning messages with yellow color
21+
function print_warning {
22+
echo -e "\033[1;33m⚠️ WARNING: $1\033[0m"
23+
}
24+
25+
# Function to check if a command exists
26+
function command_exists {
27+
command -v "$1" &>/dev/null
28+
}
29+
30+
# Ensure Git is installed
31+
if ! command_exists git; then
32+
print_error "Git is not installed. Please install Git first."
33+
exit 1
34+
fi
35+
36+
# Ensure Make is installed (if not, fall back to Go build)
37+
if ! command_exists make; then
38+
print_warning "Make is not installed. Falling back to 'go build' for compilation."
39+
if ! command_exists go; then
40+
print_error "Go is not installed either. Please install Go or Make to proceed."
41+
exit 1
42+
fi
43+
fi
44+
45+
# Clear screen before starting the Pulsarship installation
46+
clear
47+
print_info "🚀 Cloning the Pulsarship repository..."
48+
49+
# Clone Pulsarship repository and install
50+
git clone https://github.com/xeyossr/pulsarship
51+
cd pulsarship
52+
53+
# If Make is not available, use Go build
54+
if command_exists make; then
55+
print_info "⚡ Running 'make install'..."
56+
make install
57+
else
58+
print_info "⚡ Running 'go build'..."
59+
go build -ldflags "\
60+
-X 'main.version=$(git describe --tags --abbrev=0)' \
61+
-X 'main.tag=$(git describe --tags --abbrev=0)' \
62+
-X 'main.commit=$(git rev-parse --short HEAD)' \
63+
-X 'main.buildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ)' \
64+
-X 'main.buildEnv=$(go version)'" \
65+
-o pulsarship .
66+
67+
print_info "⚡ Installing to /usr/bin ..."
68+
sudo install -Dm755 pulsarship "/usr/bin/pulsarship"
69+
fi
70+
71+
# Clear screen and display final success message
72+
clear
73+
print_success "🎉 Pulsarship installation completed!"

internal/cli/commands.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,32 @@ var InitCmd = &cobra.Command{
1919
Use: "init",
2020
Short: "Prints the shell function used to execute pulsarship",
2121
}
22-
23-
var PromptCmd = &cobra.Command{
24-
Use: "prompt",
25-
Short: "",
22+
var GenConfig = &cobra.Command{
23+
Use: "gen-config",
24+
Short: "Generates a default configuration file",
2625
Run: func(cmd *cobra.Command, args []string) {
2726
path := config.GetConfigPath(flags.ConfigFlag)
28-
err := RunPrompt(path, os.Stdout)
29-
if err != nil {
30-
fmt.Fprintln(os.Stderr, "Error:", err)
27+
if err := config.WriteDefaultConfig(path); err != nil {
28+
fmt.Fprintln(os.Stderr, "Error generating config:", err)
3129
os.Exit(1)
3230
}
31+
fmt.Println("Configuration file generated at:", path)
3332
},
3433
}
3534

36-
var RightCmd = &cobra.Command{
37-
Use: "right",
38-
Short: "",
35+
var PromptCmd = &cobra.Command{
36+
Use: "prompt",
37+
Short: "Prints the full pulsarship prompt",
3938
Run: func(cmd *cobra.Command, args []string) {
4039
path := config.GetConfigPath(flags.ConfigFlag)
41-
err := RunRightPrompt(path, os.Stdout)
40+
var err error
41+
42+
if flags.ShowRight {
43+
err = RunRightPrompt(path, os.Stdout)
44+
} else {
45+
err = RunPrompt(path, os.Stdout)
46+
}
47+
4248
if err != nil {
4349
fmt.Fprintln(os.Stderr, "Error:", err)
4450
os.Exit(1)
@@ -69,3 +75,17 @@ var InitFishCmd = &cobra.Command{
6975
fmt.Println(initShell.FishInit())
7076
},
7177
}
78+
79+
func init() {
80+
RootCmd.PersistentFlags().StringVarP(&flags.ConfigFlag, "config", "c", "", "Path to the config file")
81+
RootCmd.CompletionOptions.DisableDefaultCmd = true
82+
PromptCmd.Flags().BoolVarP(&flags.ShowRight, "right", "r", false, "Print the right prompt instead of left prompt")
83+
84+
RootCmd.AddCommand(InitCmd)
85+
RootCmd.AddCommand(GenConfig)
86+
RootCmd.AddCommand(PromptCmd)
87+
88+
InitCmd.AddCommand(InitBashCmd)
89+
InitCmd.AddCommand(InitZshCmd)
90+
InitCmd.AddCommand(InitFishCmd)
91+
}

internal/cli/flags/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package flags
22

33
var ConfigFlag string
4+
var ShowRight bool

internal/config/config.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
"github.com/pelletier/go-toml/v2"
10+
env "github.com/xeyossr/pulsarship/internal"
11+
"github.com/xeyossr/pulsarship/internal/models"
12+
)
13+
14+
func ExpandPath(path string) string {
15+
if strings.HasPrefix(path, "~/") {
16+
path = filepath.Join(env.HOME_ENV, path[2:])
17+
}
18+
return path
19+
}
20+
21+
func GetConfigPath(configFlag string) string {
22+
if configFlag != "" {
23+
return ExpandPath(configFlag)
24+
}
25+
26+
if env.PULSARSHIP_CONFIG != "" {
27+
return ExpandPath(env.PULSARSHIP_CONFIG)
28+
}
29+
30+
return filepath.Join(env.HOME_ENV, ".config", "pulsarship", "pulsarship.toml")
31+
}
32+
33+
// Read and parse the configuration file at the given path.
34+
func ParseConfig(file string) (models.PromptConfig, error) {
35+
var config models.PromptConfig
36+
37+
data, err := os.ReadFile(file)
38+
if err != nil {
39+
return models.PromptConfig{}, fmt.Errorf("could not read config file: %w", err)
40+
}
41+
42+
if err := toml.Unmarshal(data, &config); err != nil {
43+
return models.PromptConfig{}, fmt.Errorf("could not parse config file: %w", err)
44+
}
45+
46+
return config, nil
47+
}
48+
49+
// Write the default config to the given path.
50+
func WriteDefaultConfig(path string) error {
51+
data, err := toml.Marshal(DefaultConfig)
52+
if err != nil {
53+
return fmt.Errorf("could not encode default config: %w", err)
54+
}
55+
56+
err = os.MkdirAll(filepath.Dir(path), 0755)
57+
if err != nil {
58+
return fmt.Errorf("could not create config directory: %w", err)
59+
}
60+
61+
err = os.WriteFile(path, data, 0644)
62+
if err != nil {
63+
return fmt.Errorf("could not write config file: %w", err)
64+
}
65+
66+
return nil
67+
}

0 commit comments

Comments
 (0)