-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4041222
Showing
7 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module teleport | ||
|
||
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
echo "$1" | ||
source "$1" |
Binary file not shown.