Skip to content

Commit

Permalink
create main.go file, fix tran version checker config import
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Feb 4, 2022
1 parent e01f7c2 commit df6a3c4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/mgutz/ansi"
"github.com/abdfnx/looker"
"github.com/abdfnx/tran/api"
"github.com/abdfnx/tran/config"
"github.com/abdfnx/tran/cmd/factory"
"github.com/abdfnx/tran/internal/config"
)

func Check(buildVersion string) {
Expand Down
93 changes: 93 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"os"
"fmt"
"errors"
"runtime"

"github.com/mgutz/ansi"
"github.com/spf13/cobra"
"github.com/abdfnx/tran/tools"
"github.com/abdfnx/tran/cmd/tran"
"github.com/abdfnx/tran/cmd/factory"
"github.com/abdfnx/tran/app/checker"
"github.com/AlecAivazis/survey/v2/terminal"
surveyCore "github.com/AlecAivazis/survey/v2/core"
)

var (
version string
buildDate string
)

type exitCode int

const (
exitOK exitCode = 0
exitError exitCode = 1
exitCancel exitCode = 2
)

func main() {
code := mainRun()
os.Exit(int(code))
}

func mainRun() exitCode {
runtime.LockOSThread()

cmdFactory := factory.New()
hasDebug := os.Getenv("DEBUG") != ""
stderr := cmdFactory.IOStreams.ErrOut

if !cmdFactory.IOStreams.ColorEnabled() {
surveyCore.DisableColor = true
} else {
surveyCore.TemplateFuncsWithColor["color"] = func(style string) string {
switch style {
case "white":
if cmdFactory.IOStreams.ColorSupport256() {
return fmt.Sprintf("\x1b[%d;5;%dm", 38, 242)
}

return ansi.ColorCode("default")

default:
return ansi.ColorCode(style)
}
}
}

if len(os.Args) > 1 && os.Args[1] != "" {
cobra.MousetrapHelpText = ""
}

RootCmd := tran.Execute(cmdFactory, version, buildDate)

if cmd, err := RootCmd.ExecuteC(); err != nil {
if err == tools.SilentError {
return exitError
} else if tools.IsUserCancellation(err) {
if errors.Is(err, terminal.InterruptErr) {
fmt.Fprint(stderr, "\n")
}

return exitCancel
}

tools.PrintError(stderr, err, cmd, hasDebug)

return exitError
}

if tran.HasFailed() {
return exitError
}

if len(os.Args) > 1 && os.Args[1] != "tran" {
checker.Check(version)
}

return exitOK
}

0 comments on commit df6a3c4

Please sign in to comment.