From 798ce32511e1ff7053e39b948e7ce5ce74cacca9 Mon Sep 17 00:00:00 2001 From: jet2tlf Date: Fri, 31 May 2024 22:27:02 -0300 Subject: codecrafters submit [skip ci] --- cmd/mygit/main.go | 55 ++++++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/cmd/mygit/main.go b/cmd/mygit/main.go index a8d2b65..2abec34 100644 --- a/cmd/mygit/main.go +++ b/cmd/mygit/main.go @@ -2,39 +2,32 @@ package main import ( "fmt" - // Uncomment this block to pass the first stage! - // "os" + "os" ) -// Usage: your_git.sh ... func main() { - // You can use print statements as follows for debugging, they'll be visible when running tests. - fmt.Println("Logs from your program will appear here!") + if len(os.Args) < 2 { + fmt.Fprintf(os.Stderr, "usage: mygit [...]\n") + os.Exit(1) + } - // Uncomment this block to pass the first stage! - // - // if len(os.Args) < 2 { - // fmt.Fprintf(os.Stderr, "usage: mygit [...]\n") - // os.Exit(1) - // } - // - // switch command := os.Args[1]; command { - // case "init": - // for _, dir := range []string{".git", ".git/objects", ".git/refs"} { - // if err := os.MkdirAll(dir, 0755); err != nil { - // fmt.Fprintf(os.Stderr, "Error creating directory: %s\n", err) - // } - // } - // - // headFileContents := []byte("ref: refs/heads/main\n") - // if err := os.WriteFile(".git/HEAD", headFileContents, 0644); err != nil { - // fmt.Fprintf(os.Stderr, "Error writing file: %s\n", err) - // } - // - // fmt.Println("Initialized git directory") - // - // default: - // fmt.Fprintf(os.Stderr, "Unknown command %s\n", command) - // os.Exit(1) - // } + switch command := os.Args[1]; command { + case "init": + for _, dir := range []string{".git", ".git/objects", ".git/refs"} { + if err := os.MkdirAll(dir, 0755); err != nil { + fmt.Fprintf(os.Stderr, "Error creating directory: %s\n", err) + } + } + + headFileContents := []byte("ref: refs/heads/main\n") + if err := os.WriteFile(".git/HEAD", headFileContents, 0644); err != nil { + fmt.Fprintf(os.Stderr, "Error writing file: %s\n", err) + } + + fmt.Println("Initialized git directory") + + default: + fmt.Fprintf(os.Stderr, "Unknown command %s\n", command) + os.Exit(1) + } } -- cgit v1.2.3