~humaid/hmsh-go

99a83f1f4c65bf45dbbfcfc72fb269c3a733ce34 — Humaid AlQassimi 1 year, 2 months ago
Initial commit for hmsh rewrite in Go
4 files changed, 139 insertions(+), 0 deletions(-)

A .build.yml
A .gitignore
A LICENSE
A main.go
A  => .build.yml +19 -0
@@ 1,19 @@
image: archlinux
packages:
  - go
sources:
  - https://git.sr.ht/~humaid/hmsh
tasks:
  - setup: |
      cd hmsh
      go get ./...
  - format: |
      cd hmsh
      go fmt $(go list ./... | grep -v /vendor/)
      go vet $(go list ./... | grep -v /vendor/)
  - test: |
      cd hmsh
      go test -cover -race $(go list ./... | grep -v /vendor/)
  - build: |
      cd hmsh
      go build

A  => .gitignore +1 -0
@@ 1,1 @@
hmsh

A  => LICENSE +25 -0
@@ 1,25 @@
BSD 2-Clause License

Copyright (c) 2019, Humaid AlQassimi
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

A  => main.go +94 -0
@@ 1,94 @@
package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"os/user"
	"regexp"
	"strings"
)

var Host, HomeDir, User string
var HomeDirRegex *regexp.Regexp
var scanner = bufio.NewScanner(os.Stdin)

func getCommandInput() []string {
	var input string
	if scanner.Scan() {
		input = scanner.Text()
	}
	return strings.Split(input, " ")
}

var (
	ANSIReset     = "\033[0m"
	ANSIRedBold   = "\033[31;1m"
	ANSIWhiteBold = "\033[97;1m"
	ANSICyan      = "\033[0;36m"
)

func printPrompt() {
	workDir, err := os.Getwd()
	if err != nil {
		fmt.Errorf("Cannot get current working directory!")
	}
	var prompt rune
	if os.Getuid() == 0 {
		prompt = '#'
	} else {
		prompt = '$'
	}
	truncatedWorkDir := HomeDirRegex.ReplaceAllLiteralString(workDir, "~")

	fmt.Printf("%s%s@%s%s:%s%s %s%c ", ANSIRedBold, User, Host, ANSIWhiteBold,
		ANSICyan, truncatedWorkDir, ANSIReset, prompt)
}

func isBuiltin(command []string) bool {
	if command[0] == "exit" {
		os.Exit(0)
		return true
	} else if command[0] == "cd" {
		if len(command) == 1 {
			os.Chdir(HomeDir)
		} else {
			os.Chdir(command[1])
		}
		return true
	}
	return false
}

func main() {
	HomeDir = os.Getenv("HOME")
	HomeDirRegex = regexp.MustCompile(fmt.Sprintf("^%s", HomeDir))
	user, err := user.Current()
	if err != nil {
		panic("Cannot get current system user")
	}
	User = user.Username
	Host, _ = os.Hostname()
	for {
		printPrompt()
		input := getCommandInput()
		if len(input) > 0 && input[0] != "" {
			var cmd *exec.Cmd
			if len(input) == 1 {
				cmd = exec.Command(input[0])
			} else if len(input) > 1 {
				cmd = exec.Command(input[0], input[1:]...)
			}
			if isBuiltin(input) {
				continue
			}
			cmd.Stdout = os.Stdout
			cmd.Stdin = os.Stdin
			cmd.Stderr = os.Stderr
			if err = cmd.Run(); err != nil {
				fmt.Printf("hmsh: %s\n", err)
			}
		}
	}
}