This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"fmt" | ||
"errors" | ||
"runtime" | ||
|
||
"github.com/mgutz/ansi" | ||
"github.com/spf13/cobra" | ||
"github.com/scmn-dev/secman/tools" | ||
"github.com/scmn-dev/secman/cmd/secman" | ||
"github.com/scmn-dev/secman/cmd/factory" | ||
"github.com/AlecAivazis/survey/v2/terminal" | ||
"github.com/scmn-dev/secman/internal/checker" | ||
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 := secman.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 secman.HasFailed() { | ||
return exitError | ||
} | ||
|
||
checker.Check(version) | ||
|
||
return exitOK | ||
} |