Skip to content

Commit

Permalink
Final changes... Error handling, ASCII Art, Credits, Args and more! 🔚
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoofreitas committed Mar 24, 2021
1 parent 1fba583 commit 0665490
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 24 deletions.
6 changes: 4 additions & 2 deletions fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ func FetchData() string {
URL := "https://raw.githubusercontent.com/karan/Projects/master/README.md"
res, err := http.Get(URL)
if err != nil {
log.Fatal(err)
log.Fatalf("Some error occurred fetching data from the web. Please check your internet connection. \n"+
"Error: %s", err.Error())
}

defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
log.Fatalf("Some error occurred while parsing the body of the request. Please restart the program.\n"+
"Error: %s", err.Error())
}

return string(body)
Expand Down
4 changes: 3 additions & 1 deletion generator/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
func GenerateDir(name string) {
err := os.Mkdir(name, 0777)
if err != nil {
log.Fatal(err)
log.Fatalf("Error while trying to create a folder. \n"+
"Please try pick other folder name or try to run this program has an administrator. \n"+
"Error: %s", err.Error())
}
}
16 changes: 8 additions & 8 deletions generator/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import (
"os"
)

func GenerateFile(filename string, extension string, content string) int {
// Add validator for file generator...

func GenerateFile(filename string, extension string, content string) {
file, err := os.Create(filename + extension)
if err != nil {
log.Fatal(err)
return 1
log.Fatalf("Error while trying to create a folder. "+
"Please try pick other folder name or try to run this program has an administrator. \n"+
"ERROR: %s", err.Error())
}

writer := bufio.NewWriter(file)
_, err = writer.WriteString(content + "\n")
if err != nil {
log.Fatalf("Got error while writing to a file. Err: %s", err.Error())
return 1
}
writer.Flush()
return 0
err = writer.Flush()
if err != nil {
log.Fatalf("Got error while writing to a file. Err: %s", err.Error())
}
}
60 changes: 49 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,57 @@ import (
"./fetcher"
"./parsers"
"./project"
"fmt"
"log"
"os"
"os/exec"
"runtime"
)

func main() {
log.Printf("%+v\n", parsers.ParsedData(fetcher.FetchData()))
project.GenerateProject(parsers.ParsedData(fetcher.FetchData()))
}

/*
Todo:
Add args for naming the main root folder
Add tree of that path
Handle possible errors
Variable Renaming
Create a nice ASCII art banner
*/
baseDir := "Project Learning"
if len(os.Args[1:]) >= 1 {
if os.Args[1] == "help" || os.Args[1] == "-h" {
fmt.Printf("HELP: \n\n" +
"To run this program just simply run it in a terminal. \n" +
"Eg: ./main \n\n" +
"If you want to run the program and name a base directory to something of your choice just simply run it " +
"using a program argument. \n" +
"Eg: ./main <NameOfBaseDir> \n" +
"without the \"<, >\" \n\n" +
"For more information please visit: https://github.com/joaoofreitas and read the README file. \n\n" +
"This software is licensed by MIT License. \n" +
"Copyright (c) 2021 João Freitas")
os.Exit(0)
} else {
baseDir = os.Args[1]
}
}

project.GenerateProject(baseDir, parsers.ParsedData(fetcher.FetchData()))

if runtime.GOOS != "windows" {
tree, err := exec.Command("tree", baseDir).CombinedOutput()
if err != nil {
log.Fatalf("Something happened while trying to print the tree of the generated folder but everything should be working.\n "+
"If you are having trouble us"+
"ing this tool please open a issue in:"+
"http://github.com/joaoofreitas\n "+
"Error: %s", err.Error())
}
fmt.Println(string(tree))
}
fmt.Print("\n\n" +
"________ __ __\n" +
"/_ __/ / ___ ____ / /__ __ _____ __ __/ /\n" +
" / / / _ \\ _ `/ _ \\/ '_/ / // / _ \\ // /_/ \n" +
"/_/ /_//_\\_,_/_//_/_/\\_\\ \\_, /\\___\\_,_(_) \n" +
" /___/ " +
"\n\n")
fmt.Println("For using free software. \n" +
"This software is licensed by MIT License. \n" +
"https://github.com/joaoofreitas" +
"https://joaoofreitas.tech" +
"Copyright (c) 2021 João Freitas")
}
3 changes: 1 addition & 2 deletions project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"path/filepath"
)

func GenerateProject(model []types.Subject) {
baseDir := "ProjectFolder"
func GenerateProject(baseDir string, model []types.Subject) {
generator.GenerateDir(baseDir)

for _, subject := range model {
Expand Down

0 comments on commit 0665490

Please sign in to comment.