Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Maziar110 committed Apr 17, 2022
0 parents commit 4041222
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Teleport
--
This script will help you to add an Alias for every directory in your Shell.
Currently it just supports Linux ( Debian Based Distributions ).

With Teleport you can set a name for each directory you want and easily access that through shell, you can set name for nested folders and access them by entering a simple word

## How to use

1. In order not to break your current profile, the script creates a new file named: `.bash_aliases ` in you home directory, you need to add below lines at the end of you `.bashrc` file in `/home/username/.bashrc` ( username should be edited to yours )
```
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
```
2. Place built package (`teleport`) in `/usr/local/bin` or in `/usr/bin/` you also can make a shortcut with `ln -s ` to make it easier :
```
ln -s <path to teleport built file> /usr/local/bin/sv
```
which `sv` will be a short name for teleport
3. Head to each directory you intend to and run `sv alias` which `sv` is the name of teleport built app you previously made a shortcut of and alias is the name you want to assign to that direction.
4. From now on you can jump to the directory easily by write your alias name.
2 changes: 2 additions & 0 deletions addressme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
talon /home/maziyar/Documents/work/talonCode/
teleport /home/maziyar/Documents/work/lab/teleport
6 changes: 6 additions & 0 deletions addressme.txt.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Tip: Each line should contain a name(alias) and a directory which already exists otherwise you will face an error
Tip: in each line just put your alias and the address only with 1 space:


alias /home/john/myDirectory/mysubdirectory/mystuffIdontwanttosee/anothersubdirectory/finalymydirectory/alias/
videos /home/myvideos/camera/1990/videos/
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module teleport

go 1.18
139 changes: 139 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package main

import (
"bufio"
"fmt"
"os"
"runtime"
"strings"
)

func main() {

user_inputs := os.Args[1:]
your_directory, err := os.Getwd()
if err != nil {
fmt.Println("An error appeared while trying to find yor location: ", err)
}
// If User enter nothing, it will print the current location
if len(user_inputs) < 1 {

fmt.Println("You are in ", your_directory)

} else {
// If user pass arguments to app
handle_request(user_inputs, your_directory)

}

}

func handle_request(arg []string, path string) {
file_path := "./addressme.txt"
if len(arg) > 1 {
fmt.Println("Input Erro, enter alias as argument withough space")
return
} else {
switch arg[0] {
case `bulk`:
fmt.Println("Will be added soon, update this file: ", file_path)
default:
command := alias_creator(arg[0], path)
update_bashrc(command)
}
}

}

func alias_creator(alias string, path string) string {
command := fmt.Sprintf("alias %s='cd %s'", alias, path)
return command
}

// Reads a file and gets the alias from it and returns a Map of "alias": "location"
func bulk_alias_from_file(path string) map[string]string {

address_book := make(map[string]string)

file, err := os.Open(path)
if err != nil {
fmt.Printf("An error while opening %s file", path)
}
defer file.Close()
lines := bufio.NewScanner(file)
for lines.Scan() {
// I call this function (lines.text()) just once and use in 2 place to avoid calling it multipe times
line_string := lines.Text()
line := strings.Split(line_string, " ")
if len(line) > 2 {
fmt.Printf("There are more spaces in line %s which we picked %s as alias and %s as address", line_string, line[0], line[1])
}
address_book[line[0]] = line[1]
}
if err := lines.Err(); err != nil {
fmt.Println("An error while reading address lines: ", err)
}
return address_book
}

func alias_exists(addresses map[string]string, key string) (bool, string) {

if address, ok := addresses[key]; ok {
return true, address
} else {
return false, "empty"
}

}

func update_bashrc(command string) bool {
dir, is_directory := where_to_save()
if is_directory {
if file, err := os.OpenFile(dir, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644); err != nil {
fmt.Printf("An error while adding Alias to %s , error is: %s", dir, err)
return false
} else {
defer file.Close()

if _, err := file.WriteString(command + "\n"); err != nil {
fmt.Println(err)
return false
} else {
source_bashrc(dir)
return true
}
}

} else {
return false
}
}

/*
Check OS and return a specific directory based on OS
*/
func where_to_save() (string, bool) {
os_name := runtime.GOOS
var bashrc_dir string
switch os_name {
case "windows":
fmt.Println("Windows operating system is not supported yet")
return "", false
case "darwin":
fmt.Println("MAC operating system is not supported yet")
return "", false
case "linux":
home_dir, err := os.UserHomeDir()
if err != nil {
fmt.Println("Home directory not found, setting default")
home_dir = "~/"
}
bashrc_dir = home_dir + "/.bash_aliases"
return bashrc_dir, true
}
return "", false
}

func source_bashrc(path string) {
fmt.Printf("to apply the change, run: `source %s` \n", path)
}
3 changes: 3 additions & 0 deletions scripts/source.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
echo "$1"
source "$1"
Binary file added teleport
Binary file not shown.

0 comments on commit 4041222

Please sign in to comment.