Skip to content

Commit

Permalink
🚀 v1.1.0 - CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
shravanasati committed Aug 6, 2021
1 parent 2b8d6dd commit ca127bd
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 76 deletions.
104 changes: 104 additions & 0 deletions axon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"fmt"
"strings"

"github.com/thatisuday/commando"
)

const (
NAME = "axon"
VERSION = "1.1.0"
)

func main() {
fmt.Println(NAME, VERSION)
deletePreviousInstallation()

// setting up executable details
commando.
SetExecutableName(NAME).
SetVersion(VERSION).
SetDescription("axon is a command line utility to organise and pretty your file system quickly and reliably.")


// root command
commando.
Register(nil).
SetShortDescription("Run axon with default options.").
SetDescription("Run axon with default options, organising all files and folders.").
AddArgument("dirs...", "The directory to be organized.", "./").
AddFlag("prettify,p", "Prettify all files with a desired casing.", commando.String, "none").
AddFlag("organise,o", "Organise the directory.", commando.Bool, true).
AddFlag("rename,r", "Rename the files numerically with a certain alias.", commando.String, "none").
SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {

// getting all arg and flag values
dirs := strings.Split(args["dirs"].Value, ",")

prettify, e := flags["prettify"].GetString()
if e != nil {
prettify = "none"
}
organise, e := flags["organise"].GetBool()
if e != nil {
organise = false
}

rename, e := flags["rename"].GetString()
if e != nil {
rename = "none"
}

// making a buffered channel
ch := make(chan string, len(dirs))

// organising the files
for _, dir := range dirs {
go func(dir string) {
if validPath(dir) {
fo := FileOrganizer{
path: dir,
}

if organise {
fo.organize()
}

if prettify != "none" {
fo.prettify(prettify)
}

if rename != "none" {
fo.renameDir(rename)
}

ch <- fo.showActions()

} else {
ch <- fmt.Sprintf("Skipping %s since it's not a valid directory.", dir)
}

}(dir)
}

// waiting for all the goroutines to finish
for i := 0; i < len(dirs); i++ {
fmt.Println(<-ch)
}

})


// up command
commando.
Register("up").
SetShortDescription("Update axon.").
SetDescription("Update axon to the latest version.").
SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
update()
})

commando.Parse(nil)
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/Shravan-1908/File-Organizer
module github.com/Shravan-1908/axon

go 1.16

require github.com/thatisuday/commando v1.0.4
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/thatisuday/clapper v1.0.10 h1:1EkqE/nb4npp8DuTKnpvVzO/Mcac9lOPND34uUKF+bU=
github.com/thatisuday/clapper v1.0.10/go.mod h1:FQGIg8q2uzeI+3SUS82YKF4E3KexkHStbiK4qTfDknM=
github.com/thatisuday/commando v1.0.4 h1:aNdH9tvmx2EPG6rT3NTQOV/qFYPf4Ap4Spo+q+n9Ois=
github.com/thatisuday/commando v1.0.4/go.mod h1:ODGz6jwJs4QqhLJtCjRRs8xIrmLLMdatYYddP+v1b4E=
24 changes: 24 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/bash

platforms=("windows/amd64" "darwin/amd64" "linux/amd64")

for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name="./bin/axon-$GOOS-$GOARCH"
if [ $GOOS = "windows" ]; then
output_name+='.exe'
fi

echo "==> Building executable for $GOOS/$GOARCH..."

env GOOS=$GOOS GOARCH=$GOARCH go build -v -ldflags="-s -w" -o $output_name
if [ $? -ne 0 ]; then
echo '==> An error has occurred! Aborting the script execution...'
exit 1
fi
done

echo "==> Executables built successfully!"
16 changes: 16 additions & 0 deletions scripts/linux_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

echo "Downloading axon..."
curl -L "https://github.com/Shravan-1908/axon/releases/latest/download/axon-linux-amd64" -o axon

echo "Adding axon into PATH..."

mkdir -p ~/.axon

chmod u+x ./axon

mv ./axon ~/.axon
echo "export PATH=$PATH:~/.axon" >> ~/.bashrc

echo "axon installation is completed!"
echo "You need to restart the shell to use axon."
14 changes: 14 additions & 0 deletions scripts/macos_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#!/bin/bash

echo "Downloading axon..."
curl -L "https://github.com/Shravan-1908/axon/releases/latest/download/axon-darwin-amd64" -o axon

echo "Adding axon into PATH..."

mkdir -p ~/.axon;
mv ./axon ~/.axon
echo "export PATH=$PATH:~/.axon" >> ~/.bashrc

echo "axon installation is completed!"
echo "You need to restart the shell to use axon."
18 changes: 18 additions & 0 deletions scripts/windows_install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Write-Host "Downloading axon..."

$url = "https://github.com/Shravan-1908/axon/releases/latest/download/axon-windows-amd64.exe"

$dir = $env:USERPROFILE + "\.axon"
$filepath = $env:USERPROFILE + "\.axon\axon.exe"

[System.IO.Directory]::CreateDirectory($dir)
(Invoke-WebRequest -Uri $url -OutFile $filepath)

Write-Host "Adding axon to PATH..."
[Environment]::SetEnvironmentVariable(
"Path",
[Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine) + ";"+$dir,
[EnvironmentVariableTarget]::Machine)

Write-Host "axon installation is successful!"
Write-Host "You need to restart your shell to use axon."
95 changes: 95 additions & 0 deletions up.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
)

// Update updates axon by downloading the latest executable from github, and renaming the
// old executable to `axon-old` so that it can be deleted by `DeletePreviousInstallation`.
func update() {
fmt.Println("Updating axon...")
fmt.Println("Downloading the axon executable...")

// * determining the os-specific url
url := ""
switch runtime.GOOS {
case "windows":
url = "https://github.com/Shravan-1908/axon/releases/latest/download/axon-windows-amd64.exe"
case "linux":
url = "https://github.com/Shravan-1908/axon/releases/latest/download/axon-linux-amd64"
case "darwin":
url = "https://github.com/Shravan-1908/axon/releases/latest/download/axon-darwin-amd64"
default:
fmt.Println("Your OS isn't supported by axon.")
return
}

// * sending a request
res, err := http.Get(url)

if err != nil {
fmt.Println("Error: Unable to download the executable. Check your internet connection.")
fmt.Println(err.Error())
return
}

defer res.Body.Close()

// * determining the executable path
downloadPath, e := os.UserHomeDir()
if e != nil {
fmt.Println("Error: Unable to determine axon's location.")
fmt.Println(e.Error())
return
}
downloadPath += "/.axon/axon"
if runtime.GOOS == "windows" {
downloadPath += ".exe"
}

os.Rename(downloadPath, downloadPath+"-old")

exe, er := os.Create(downloadPath)
if er != nil {
fmt.Println("Error: Unable to access file permissions.")
fmt.Println(er.Error())
return
}
defer exe.Close()

// * writing the received content to the axon executable
_, errr := io.Copy(exe, res.Body)
if errr != nil {
fmt.Println("Error: Unable to write the executable.")
fmt.Println(errr.Error())
return
}

// * performing an additional `chmod` utility for linux and mac
if runtime.GOOS == "darwin" || runtime.GOOS == "linux" {
os.Chmod(downloadPath, 0755)
}

fmt.Println("axon was updated successfully.")
}

// DeletePreviousInstallation deletes previous installation if it exists.
func deletePreviousInstallation() {
axonDir, _ := os.UserHomeDir()
axonDir += "/.axon"

files, _ := ioutil.ReadDir(axonDir)
for _, f := range files {
if strings.HasSuffix(f.Name(), "-old") {
// fmt.Println("found existsing installation")
os.Remove(axonDir + "/" + f.Name())
}
// fmt.Println(f.Name())
}
}
Loading

0 comments on commit ca127bd

Please sign in to comment.