Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
blobbybilb committed Feb 23, 2024
1 parent 5e3a0b1 commit 6a19ec6
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 48 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

65 changes: 58 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,74 @@ A CLI utility to make cross-shell command aliases, and access them from a simple
<img width="397" alt="Screenshot 2023-07-11 at 5 06 16 PM" src="https://github.com/blobbybilb/falsename/assets/58201828/60134ce0-b157-4b5a-b70c-05e82ee4ae4f">


## Status
- **V1:** Update to V2 (shouldn't break anything).
- **V2:** Adds custom config locations (for syncing, etc.), and passes arguments to command when running an alias by name. *Current version, complete.*

It's feature complete as far as I had planned, there might be a V3 if I think a new feature is necessary (feel free to suggest!).

## Download

Pre-built binaries for Windows, macOS, Linux, and FreeBSD are available in the build folder in the repository.

## Status
- **V1:** Complete. Bug fixes/maintenence only.
- V2? Maybe.
```sh
# macOS - Apple Silicon
curl -L https://github.com/blobbybilb/falsename/raw/main/build/mac-arm64/fn -O
# macOS - Intel
curl -L https://github.com/blobbybilb/falsename/raw/main/build/mac-amd64/fn -O

# Linux - amd64
curl -L https://github.com/blobbybilb/falsename/raw/main/build/linux-amd64/fn -O
# Linux - arm64
curl -L https://github.com/blobbybilb/falsename/raw/main/build/linux-arm64/fn -O

# FreeBSD - amd64
curl -L https://github.com/blobbybilb/falsename/raw/main/build/freebsd-amd64/fn -O
# FreeBSD - arm64
curl -L https://github.com/blobbybilb/falsename/raw/main/build/freebsd-arm64/fn -O

# Windows - amd64
curl -L https://github.com/blobbybilb/falsename/raw/main/build/windows-amd64/fn.exe -O
```

Then, make it executable and move it to a directory in your PATH.

```sh
# non-Windows (macOS, Linux, FreeBSD)
chmod +x fn
sudo mv fn /usr/local/bin
```

Finally, set your config directory (optional, default is `~/.config/falsename`) (useful if you have a directory synced across computers or the like).

```sh
fn config ~/new/config/directory
```

## Usage:

- `fn` -> choose an alias from a list of all aliases
- `fn <alias>` -> run an alias
- `fn <alias> [args]` -> run an alias with optional arguments
- `fn list` -> list all aliases
- `fn get <alias>` -> get the command for an alias
- `fn save <alias> <command>` -> save an alias
- `fn shell` -> get configured shell
- `fn shell <shell>` -> set shell
- `fn shell` -> get configured shell (default /bin/sh)
- `fn shell <shell>` -> set shell (not recommended unless you don't have /bin/sh)
- `fn delete <alias>` -> delete an alias
- `fn config` -> get the config directory
- `fn config <dir>` -> set the config directory

### Example:

```sh
fn save hi "echo hello"
fn hi # -> hello
fn # choose from a list of all aliases
fn list # list all aliases
fn get hi # get the command for an alias
fn delete hi # delete an alias
```

##### License: GNU GPLv3

###### License: GNU GPLv3
*"run(ning) an alias" doesn't sound right...*
Binary file modified build/freebsd-amd64/fn
Binary file not shown.
Binary file modified build/freebsd-arm64/fn
Binary file not shown.
Binary file modified build/linux-amd64/fn
Binary file not shown.
Binary file modified build/linux-arm64/fn
Binary file not shown.
Binary file modified build/mac-amd64/fn
Binary file not shown.
Binary file modified build/mac-arm64/fn
Binary file not shown.
Binary file modified build/windows-amd64/fn.exe
Binary file not shown.
8 changes: 3 additions & 5 deletions choose/choose.go → choose.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package choose
package main

import (
"falsename/types"

"github.com/nsf/termbox-go"
)

func DisplayAliasesMenu(options []types.Command, selected int) {
func DisplayAliasesMenu(options []Command, selected int) {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
for i, option := range options {
offsetX := 2
Expand All @@ -25,7 +23,7 @@ func DisplayAliasesMenu(options []types.Command, selected int) {
termbox.Flush()
}

func ChooseAlias(options []types.Command) int {
func ChooseAlias(options []Command) int {
termbox.Init()
defer termbox.Close()

Expand Down
39 changes: 30 additions & 9 deletions data/data.go → data.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
package data
package main

import (
"falsename/types"
"os"
"strings"

"gopkg.in/yaml.v3"
)

var (
DataDirPath = os.Getenv("HOME") + "/.config/.falsename/"
// DataDirPath = "tmpdata/"
// configDirPath = "tmpdata/"
configDirPath = os.Getenv("HOME") + "/.config/falsename/"
DataDirPath = configDirPath
)

func init() {
if _, err := os.Stat(DataDirPath); os.IsNotExist(err) {
os.MkdirAll(DataDirPath, 0755)
if _, err := os.Stat(configDirPath); os.IsNotExist(err) {
os.MkdirAll(configDirPath, 0755)
}

dataDirPathFile := configDirPath + "data_dir_path.txt"
if _, err := os.Stat(dataDirPathFile); err == nil {
data, err := os.ReadFile(dataDirPathFile)
if err == nil {
DataDirPath = strings.TrimSpace(string(data))
if !strings.HasSuffix(DataDirPath, "/") {
DataDirPath += "/"
}
}
}
}

func SetDataDirPath(path string) {
DataDirPath = path
os.WriteFile(configDirPath+"data_dir_path.txt", []byte(DataDirPath), 0644)
}

func SaveCommand(name, command string) {
commands := GetAllCommands()

Expand All @@ -32,7 +49,11 @@ func SaveCommand(name, command string) {
}

if !foundExisting {
commands = append(commands, types.Command{Name: name, Command: command})
commands = append(commands, Command{Name: name, Command: command})
}

if _, err := os.Stat(DataDirPath); os.IsNotExist(err) {
os.MkdirAll(DataDirPath, 0755)
}

f, _ := os.Create(DataDirPath + "aliases.yml")
Expand All @@ -52,11 +73,11 @@ func GetCommand(name string) string {
return "--not found--"
}

func GetAllCommands() []types.Command {
func GetAllCommands() []Command {
f, _ := os.Open(DataDirPath + "aliases.yml")
defer f.Close()

var commands []types.Command
var commands []Command
dec := yaml.NewDecoder(f)
dec.Decode(&commands)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ require (
)

require (
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
60 changes: 41 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"falsename/choose"
"falsename/data"
"flag"
"fmt"
"os"
Expand All @@ -22,41 +20,41 @@ func main() {
}

if flag.NArg() == 0 { // choose
commands := data.GetAllCommands()
selectedAlias := choose.ChooseAlias(commands)
commands := GetAllCommands()
selectedAlias := ChooseAlias(commands)
if selectedAlias == -1 {
return
}
if selectedAlias == -2 {
fmt.Println("No aliases found. Use fn save to save a command.")
fmt.Println("No aliases found. Use 'fn save' to save a command.")
return
}
runCommand(commands[selectedAlias].Command)
return
}
if flag.Arg(0) == "list" { // list
commands := data.GetAllCommands()
commands := GetAllCommands()
for _, cmd := range commands {
fmt.Printf("%s: %s\n", cmd.Name, cmd.Command)
}
return
}

if flag.Arg(0) == "get" { // get
cmd := data.GetCommand(flag.Arg(1))
cmd := GetCommand(flag.Arg(1))
fmt.Println(cmd)
return
}

if flag.Arg(0) == "shell" { // shell
if flag.NArg() == 1 {
shell := data.GetShell()
shell := GetShell()
fmt.Println(shell)
return
}

if flag.NArg() == 2 {
data.SetShell(flag.Arg(1))
SetShell(flag.Arg(1))
return
}
}
Expand All @@ -67,7 +65,7 @@ func main() {
}

if flag.Arg(0) == "delete" { // delete
data.DeleteCommand(flag.Arg(1))
DeleteCommand(flag.Arg(1))
return
}

Expand All @@ -77,17 +75,38 @@ func main() {
return
}

data.SaveCommand(flag.Arg(1), flag.Arg(2))
SaveCommand(flag.Arg(1), flag.Arg(2))
return
}

if flag.NArg() == 1 { // run
cmd := data.GetCommand(flag.Arg(0))
runCommand(cmd)
if flag.Arg(0) == "config" {
if flag.NArg() == 1 {
fmt.Println(DataDirPath)
return
}

if flag.NArg() == 2 {
DataDirPath = flag.Arg(1)
SetDataDirPath(DataDirPath)
return
}
}

if flag.NArg() >= 1 { // run
cmd := GetCommand(flag.Arg(0))
if cmd == "--not found--" {
fmt.Println("Alias not found. Use fn save to save a command.")
return
}

remainingArgs := flag.Args()[1:]
argStr := ""
for _, arg := range remainingArgs {
argStr += " " + arg
}

cmd += argStr
runCommand(cmd)
return
}

Expand All @@ -100,24 +119,27 @@ falsename - a simple cross-shell command aliaser
Usage:
fn -> choose an alias
fn <alias> -> run an alias
fn <alias> [args] -> run an alias with optional arguments
fn list -> list all aliases
fn get <alias> -> get the command for an alias
fn save <alias> <command> -> save an alias
fn shell <shell> -> set shell
fn shell -> get configured shell
fn delete <alias> -> delete an alias
fn shell -> get configured shell (default /bin/sh)
fn shell <shell> -> set shell (not recommended unless you don't have /bin/sh)
fn config -> get the config directory
fn config <dir> -> set the config directory
The config directory is %s
`, data.DataDirPath)
`, DataDirPath)
fmt.Println(configText)
}

func runCommand(cmdStr string) {
if cmdStr == "--not found--" {
fmt.Println("Alias not found. Use fn save to save a command.")
return
}
cmd := exec.Command(data.GetShell(), "-c", cmdStr)
cmd := exec.Command(GetShell(), "-c", cmdStr)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
2 changes: 1 addition & 1 deletion types/types.go → types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package main

type Command struct {
Name string
Expand Down

0 comments on commit 6a19ec6

Please sign in to comment.