Skip to content

Commit

Permalink
Added logic to backup .zshrc on kali linux
Browse files Browse the repository at this point in the history
 - .zshrc moved to .zshrc_pre_pimpmyshell.bak
     - only when kali version in /etc/os-release >= 2020.4
     - use semver for semantic version detection
 - Updated Changelog
  • Loading branch information
Phillip Miller committed Oct 17, 2021
1 parent 7ff28bf commit f2a9a88
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [v1.5.1](https://github.com/mr-pmillz/pimp-my-shell/tree/v1.5.1) (2021-10-16)

[Full Changelog](https://github.com/mr-pmillz/pimp-my-shell/compare/v1.5.0...v1.5.1)

## [v1.5.0](https://github.com/mr-pmillz/pimp-my-shell/tree/v1.5.0) (2021-10-16)

[Full Changelog](https://github.com/mr-pmillz/pimp-my-shell/compare/v1.4.9...v1.5.0)
Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

[![Donate](https://img.shields.io/badge/Donate-PayPal-yellow.svg)](https://www.paypal.com/donate?business=YR6C4WB5CDZZL&no_recurring=0&item_name=contribute+to+open+source&currency_code=USD)

Table of Contents
=================

* [Pimp\-My\-Shell](#pimp-my-shell)
* [Install](#install)
* [Usage](#usage)
* [About](#about)
* [Resources](#resources)
* [Tmux Hotkeys](#tmux-hotkeys)
* [VIM Hotkeys](#vim-hotkeys)
* [Adjusting](#adjusting)
* [Custom Aliases](#custom-aliases)
* [Mac Fix Terminal bind keys](#mac-fix-terminal-bind-keys)
* [Enjoy](#enjoy)


![pimp-my-shell.png](imgs/pimp-my-shell.png)

## Install
Expand All @@ -28,8 +44,7 @@ p10k configure

## About

Pimp-My-Shell was created to auto configure various terminal apps that I've found myself having to manually setup many times.
pimp-my-shell aims to automate this process.
This project was designed to automate all the configurations that I typically set up for my terminal on Macos and Debian/Ubuntu Linux.

- What it does

Expand All @@ -39,6 +54,7 @@ Currently, this will (if not already installed and setup)
- install vim + awesome vim setup + plugins
- install cheat + configure + community cheatsheets
- fzf + bat for finding files fast + file preview CTRL+r search history stupendously
- and various other dependencies

## Resources

Expand Down
2 changes: 1 addition & 1 deletion extra/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package extra
import (
"embed"
"fmt"
"gopkg.in/ini.v1"
"pimp-my-shell/githubapi"
"pimp-my-shell/localio"
"gopkg.in/ini.v1"
)

//go:embed templates/*
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module pimp-my-shell
go 1.16

require (
github.com/Masterminds/semver v1.5.0
github.com/google/go-github/v39 v39.1.0
github.com/google/periph v3.6.8+incompatible
github.com/jubnzv/go-tmux v0.0.0-20210107170159-c6ae3ccbe820
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
106 changes: 106 additions & 0 deletions osrelease/osrelease.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package osrelease

import (
"bufio"
"errors"
"os"
"strings"
)

// EtcOsRelease ...
const EtcOsRelease string = "/etc/os-release"

// UsrLibOsRelease ...
const UsrLibOsRelease string = "/usr/lib/os-release"

// Read and return os-release, trying EtcOsRelease, followed by UsrLibOsRelease.
// err will contain an error message if neither file exists or failed to parse
func Read() (osrelease map[string]string, err error) {
osrelease, err = ReadFile(EtcOsRelease)
if err != nil {
osrelease, err = ReadFile(UsrLibOsRelease)
}
return
}

// ReadFile Similar to Read(), but takes the name of a file to load instead
func ReadFile(filename string) (osrelease map[string]string, err error) {
osrelease = make(map[string]string)
err = nil

lines, err := parseFile(filename)
if err != nil {
return
}

for _, v := range lines {
key, value, err := parseLine(v)
if err == nil {
osrelease[key] = value
}
}
return
}

func parseFile(filename string) (lines []string, err error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}

func parseLine(line string) (key string, value string, err error) {
err = nil

// skip empty lines
if len(line) == 0 {
err = errors.New("Skipping: zero-length")
return
}

// skip comments
if line[0] == '#' {
err = errors.New("Skipping: comment")
return
}

// try to split string at the first '='
splitString := strings.SplitN(line, "=", 2)
if len(splitString) != 2 {
err = errors.New("Can not extract key=value")
return
}

// trim white space from key and value
key = splitString[0]
key = strings.Trim(key, " ")
value = splitString[1]
value = strings.Trim(value, " ")

// Handle double quotes
if strings.ContainsAny(value, `"`) {
first := string(value[0:1])
last := string(value[len(value)-1:])

if first == last && strings.ContainsAny(first, `"'`) {
value = strings.TrimPrefix(value, `'`)
value = strings.TrimPrefix(value, `"`)
value = strings.TrimSuffix(value, `'`)
value = strings.TrimSuffix(value, `"`)
}
}

// expand anything else that could be escaped
value = strings.Replace(value, `\"`, `"`, -1)
value = strings.Replace(value, `\$`, `$`, -1)
value = strings.Replace(value, `\\`, `\`, -1)
value = strings.Replace(value, "\\`", "`", -1)
return
}
33 changes: 33 additions & 0 deletions zsh/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package zsh
import (
"embed"
"fmt"
"github.com/Masterminds/semver"
"io/ioutil"
"os"
"pimp-my-shell/localio"
"pimp-my-shell/osrelease"
"regexp"
"strings"
)
Expand Down Expand Up @@ -53,6 +55,37 @@ func InstallOhMyZsh(osType string, dirs *localio.Directories) error {
if err = localio.DownloadFile(dest, ohMyZshInstallScriptURL); err != nil {
return err
}

switch osType {
case "linux":
if exists, err = localio.Exists(fmt.Sprintf("%s/.zshrc", dirs.HomeDir)); err == nil && exists {
// Kali linux weird zshrc constraint
osINFO, err := osrelease.Read()
if err != nil {
return err
}
if osINFO["ID"] == "kali" {
kaliZshConstraint, err := semver.NewConstraint(">= 2020.4")
if err != nil {
return err
}
currentOSReleaseID, err := semver.NewVersion(osINFO["VERSION"])
if err != nil {
return err
}
isKaliLaterThan20204 := kaliZshConstraint.Check(currentOSReleaseID)
if isKaliLaterThan20204 {
fmt.Println("Your Kali version >= 2020.4 has highly custom .zshrc. Moving to ~/.zshrc_pre_pimpmyshell.bak")
if err = os.Rename(fmt.Sprintf("%s/.zshrc", dirs.HomeDir), fmt.Sprintf("%s/.zshrc_pre_pimpmyshell.bak", dirs.HomeDir)); err != nil {
return err
}
}
}
}
default:
// Do Nothing
}

if err = localio.RunCommandPipeOutput(fmt.Sprintf("cd %s && sh %s --keep-zshrc --unattended || true", dirs.HomeDir, dest)); err != nil {
return err
}
Expand Down

0 comments on commit f2a9a88

Please sign in to comment.