Skip to content

Commit

Permalink
added auto clean on start && fix github dir resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Simple-MAX committed Sep 29, 2022
1 parent a2b04cf commit 898b31f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
24 changes: 24 additions & 0 deletions cmd/proto-port/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/TechMDW/ProtoPort/internal/github"
"github.com/TechMDW/ProtoPort/internal/protoc"
"github.com/TechMDW/ProtoPort/internal/utilities"
)

type Command struct {
Expand All @@ -31,6 +32,29 @@ var (
)

func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
configPath, err := os.UserConfigDir()

if err != nil {
log.Println(err)
}

techMDWPath := filepath.Join(configPath, "TechMDW")

if _, err := os.Stat(techMDWPath); err != nil {
if os.IsNotExist(err) {
err := os.Mkdir(techMDWPath, 0755)
if err != nil {
log.Fatal(err)
}
}
}

ProtoPortPath := filepath.Join(techMDWPath, "ProtoPort")

// clear the previous files
utilities.DeleteAll(ProtoPortPath)

if err := root(os.Args[1:]); err != nil {
log.Println(err)
os.Exit(1)
Expand Down
4 changes: 4 additions & 0 deletions internal/github/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func GithubUrlParser(url string, subpath string) string {

url = strings.Replace(url, "/tree/main", "/contents", 1)

if !strings.Contains(url, "/contents") {
url = url + "/contents"
}

if subpath != "" {
url = strings.Replace(url, "https://github.com/", "https://api.github.com/repos/", 1) + subpath
} else {
Expand Down
9 changes: 2 additions & 7 deletions internal/protoc/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ func ReadDirForProto(path string, output string, lang string, github bool) error
for _, file := range files {
if file.IsDir() {
subdir := filepath.Join(output, file.Name())
if github {
if _, err := os.Stat(subdir); os.IsNotExist(err) {
os.Mkdir(subdir, 0755)
}
if _, err := os.Stat(subdir); os.IsNotExist(err) {
os.Mkdir(subdir, 0755)
}
ReadDirForProto(filepath.Join(path, file.Name()), subdir, lang, github)
}
Expand All @@ -51,9 +49,6 @@ func BuildProto(protoPath, output, protoFile string, lang string) error {
var command *exec.Cmd

fullProtoFilePath := filepath.Join(protoPath, protoFile)
if _, err := os.Stat(output); os.IsNotExist(err) {
os.Mkdir(output, 0755)
}

if lang == "" {
return fmt.Errorf("language not specified")
Expand Down
21 changes: 21 additions & 0 deletions internal/utilities/helper.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
package utilities

import (
"os"
"path/filepath"
"strings"
)

func CheckForFileExtension(name string, extension string) bool {
return strings.HasSuffix(name, extension)
}

func DeleteAll(path string) error {
d, err := os.Open(path)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(path, name))
if err != nil {
return err
}
}
return nil
}

func CheckIfLangIsSupported(lang string) bool {
// ckeck if lang is supported
supported := []string{"go", "cpp", "csharp", "java", "python", "ruby", "pyi", "php", "objc", "kotlin", "node", "dart"}
Expand Down

0 comments on commit 898b31f

Please sign in to comment.