Giter Club home page Giter Club logo

go-shell's Introduction

License People Stars Forks Watches Last Updated report

Short Sweet Headline πŸŽ‡πŸŽ‰

go-shell in action

go-shell is a simple command-line shell built with Go that allows developers and enthusiasts to explore and execute built-in and external commands efficiently.

Table of Contents

✨ Features

  • Built-in commands like exit, echo, pwd, cd, ls, and type.
  • Supports execution of external commands available in the PATH.
  • Simple and intuitive interface.
  • Lightweight and fast, leveraging Go's concurrency model.

⚑ Setup

βš™οΈ Requirements

  • Go 1.22 or higher
  • Git

πŸ’» Installation

Installing go-shell is as simple as cloning the repository and running the build command!

git clone https://github.com/sanurb/go-shell
cd go-shell
go build -o go-shell main.go

πŸš€ Usage

USAGE:
    go-shell
Example:
    go-shell
    $ echo Hello, World!
    Hello, World!

πŸ—οΈ What's Next

Planning to add more advanced features and improvements.

βœ… To-Do

  • Setup repo
  • Implement basic built-in commands
  • Support external command execution
  • Add support for piping and redirection
  • Enhance error handling and user feedback
  • Write comprehensive tests

πŸ§‘β€πŸ’» Behind The Code

🌈 Inspiration

go-shell was inspired by the desire to deeply understand how command-line shells work and to provide a tool that developers can use and extend. As Richard Feynman said, β€œWhat I cannot create, I do not understand”. This project embodies the philosophy that real understanding comes from building and creating.

πŸ’‘ Challenges/Learnings

  • Challenges: Ensuring compatibility with various external commands and handling different edge cases in command execution.
  • Learnings: Deepened understanding of Go's os/exec package, process management, application of some design patterns like factory pattern using go

Design Decisions for Enhanced Clarity and Extensibility

The diagram showcases a refined class structure for our interactive shell. Key decisions were made to improve code organization and maintainability:

  1. Command Interface: This establishes a contract for all commands, ensuring consistent behavior and enabling seamless addition of new commands in the future.

  2. BaseCommand Class: This provides default implementations for common Command methods, reducing boilerplate code in concrete command classes.

  3. BuiltinCommand and ExternalCommand: These abstract classes categorize commands based on their origin (internal to the shell or external system commands), enhancing clarity and separation of concerns.

  4. LsCommand Specialization: LsCommand is further specialized due to its unique handling of options and arguments, showcasing the flexibility of the Command pattern to accommodate varying command behaviors.

  5. CompositeCommand for Complex Operations: This class enables the execution of multiple commands as a single unit, promoting modularity and reusability.

  6. CommandFactory for Instantiation: This class decouples command creation from the rest of the system, allowing for easy extension and modification of command types.

  7. CommandParser for Interpretation: This class parses user input and delegates command creation to the CommandFactory, promoting a clean separation of responsibilities.

This structure facilitates future enhancements, such as the integration of new command types or the implementation of additional features.

classDiagram
    class Command {
        <<interface>>
        Execute() error
        SetStdin(io.Reader)
        SetStdout(io.Writer)
        StdinPipe() (io.WriteCloser, error)
        StdoutPipe() (io.ReadCloser, error)
    }

    class BaseCommand {
        Stdin io.Reader
        Stdout io.Writer
        SetStdin(io.Reader)
        SetStdout(io.Writer)
        StdinPipe() (io.WriteCloser, error)
        StdoutPipe() (io.ReadCloser, error)
    }

    class BuiltinCommand {
        <<abstract>>
    }

    class LsCommand {
        Options LsOptions
        DirPath string
        SetArgs([]string)
        listDirectory() error
        printLongFormat(fs.DirEntry)
    }

    class OtherBuiltinCommand {
        <<abstract>>
    }

    class ExternalCommand {
        <<abstract>>
    }

    class CompositeCommand {
        commands []Command
        Add(Command)
        Execute() error
        SetStdin(io.Reader)
        SetStdout(io.Writer)
        StdinPipe() (io.WriteCloser, error)
        StdoutPipe() (io.ReadCloser, error)
    }

    class CommandFactory {
        commands map[string]func([]string, io.Writer) Command
        Create(string, []string) (Command, error)
        Register(string, func([]string, io.Writer) Command)
    }

    class CommandParser {
        factory CommandFactory
        Parse(string) (Command, error)
    }

    class Shell {
        parser CommandParser
        Run()
    }

    Command <|-- BaseCommand
    BaseCommand <|-- BuiltinCommand
    BuiltinCommand <|-- LsCommand
    BuiltinCommand <|-- OtherBuiltinCommand
    BaseCommand <|-- ExternalCommand
    Command <|-- CompositeCommand
    CommandFactory o-- Command
    CommandParser o-- CommandFactory
    Shell o-- CommandParser

    note for LsCommand "Handles specific options and arguments"
    note for OtherBuiltinCommand "Examples: CdCommand, EchoCommand, etc."
    note for ExternalCommand "Examples: Operating system commands"
Loading

πŸ“‘ Useful Resources

⭐ hit the star button if you found this useful ⭐

Source | LinkedIn | Other Projects

go-shell's People

Contributors

sanurb avatar imgbotapp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.