Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Revert "Ban functions that exit the process (#5359)" (#5365)" #5403

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# see https://golangci-lint.run/usage/configuration/ for documentation
linters:
enable:
- forbidigo
linters-settings:
forbidigo:
# Forbid the following identifiers (list of regexp)
#
# Add a comment to the offending line with '//nolint:forbidigo' to suppress the rule if
# you're working inside the main() function.
forbid:
# These APIs exit the process. These are OK to use in the main() function,
# and not ANYWHERE else.
- 'os\.Exit(# Do not use except for inside main(). Suppress this message with //nolint:forbidigo at the end of the line if it is correct.)?'
- 'log\.Fatal(f|ln)?(# Do not use except for inside main(). Suppress this message with //nolint:forbidigo at the end of the line if it is correct.)?'
8 changes: 4 additions & 4 deletions cmd/appcore-async/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ func main() {
defaultConfig := fmt.Sprintf("radius-%s.yaml", hostoptions.Environment())
flag.StringVar(&configFile, "config-file", defaultConfig, "The service configuration file.")
if configFile == "" {
log.Fatal("config-file is empty.")
log.Fatal("config-file is empty.") //nolint:forbidigo // this is OK inside the main function.
}

flag.Parse()

options, err := hostoptions.NewHostOptionsFromEnvironment(configFile)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}

logger, flush, err := ucplog.NewLogger(logging.AppCoreLoggerName, &options.Config.Logging)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
defer flush()

Expand Down Expand Up @@ -95,7 +95,7 @@ func main() {
// gracefully, so just crash if that happens.
err = <-stopped
if err == nil {
os.Exit(0)
os.Exit(0) //nolint:forbidigo // this is OK inside the main function.
} else {
panic(err)
}
Expand Down
24 changes: 14 additions & 10 deletions cmd/appcore-rp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ import (

const serviceName = "applications.core"

func newLinkHosts(configFile string, enableAsyncWorker bool) ([]hosting.Service, *hostoptions.HostOptions) {
func newLinkHosts(configFile string, enableAsyncWorker bool) ([]hosting.Service, *hostoptions.HostOptions, error) {
hostings := []hosting.Service{}
options, err := hostoptions.NewHostOptionsFromEnvironment(configFile)
if err != nil {
log.Fatal(err)
return nil, nil, err
}
hostings = append(hostings, link_frontend.NewService(options))
if enableAsyncWorker {
hostings = append(hostings, link_backend.NewService(options))
}

return hostings, &options
return hostings, &options, nil
}

func main() {
Expand All @@ -64,14 +64,14 @@ func main() {
flag.StringVar(&linkConfigFile, "link-config", defaultLinkConfig, "The service configuration file for Applications.Link.")

if configFile == "" {
log.Fatal("config-file is empty.")
log.Fatal("config-file is empty.") //nolint:forbidigo // this is OK inside the main function.
}

flag.Parse()

options, err := hostoptions.NewHostOptionsFromEnvironment(configFile)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
hostingSvc := []hosting.Service{frontend.NewService(options)}

Expand All @@ -88,7 +88,7 @@ func main() {

logger, flush, err := ucplog.NewLogger(logging.AppCoreLoggerName, &options.Config.Logging)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
defer flush()

Expand All @@ -102,7 +102,11 @@ func main() {
if runLink && linkConfigFile != "" {
logger.Info("Run Applications.Link.")
var linkSvcs []hosting.Service
linkSvcs, linkOpts = newLinkHosts(linkConfigFile, enableAsyncWorker)
var err error
linkSvcs, linkOpts, err = newLinkHosts(linkConfigFile, enableAsyncWorker)
if err != nil {
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
hostingSvc = append(hostingSvc, linkSvcs...)
}

Expand Down Expand Up @@ -136,11 +140,11 @@ func main() {
tracerOpts.ServiceName = serviceName
shutdown, err := trace.InitTracer(tracerOpts)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
defer func() {
if err := shutdown(ctx); err != nil {
log.Fatal("failed to shutdown TracerProvider: %w", err)
log.Printf("failed to shutdown TracerProvider: %v\n", err)
}

}()
Expand All @@ -165,7 +169,7 @@ func main() {
// gracefully, so just crash if that happens.
err = <-stopped
if err == nil {
os.Exit(0)
os.Exit(0) //nolint:forbidigo // this is OK inside the main function.
} else {
panic(err)
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/applink-rp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ func main() {
flag.BoolVar(&enableAsyncWorker, "enable-asyncworker", true, "Flag to run async request process worker (for private preview and dev/test purpose).")

if configFile == "" {
log.Fatal("config-file is empty.")
log.Fatal("config-file is empty.") //nolint:forbidigo // this is OK inside the main function.
}

flag.Parse()

options, err := hostoptions.NewHostOptionsFromEnvironment(configFile)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
hostingSvc := []hosting.Service{frontend.NewService(options)}

Expand All @@ -64,7 +64,7 @@ func main() {

logger, flush, err := ucplog.NewLogger(logging.AppLinkLoggerName, &options.Config.Logging)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
defer flush()

Expand Down Expand Up @@ -98,11 +98,11 @@ func main() {
tracerOpts.ServiceName = serviceName
shutdown, err := trace.InitTracer(tracerOpts)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
defer func() {
if err := shutdown(ctx); err != nil {
log.Fatal("failed to shutdown TracerProvider: %w", err)
log.Printf("failed to shutdown TracerProvider: %v\n", err)
rynowak marked this conversation as resolved.
Show resolved Hide resolved
}

}()
Expand All @@ -127,7 +127,7 @@ func main() {
// gracefully, so just crash if that happens.
err = <-stopped
if err == nil {
os.Exit(0)
os.Exit(0) //nolint:forbidigo // this is OK inside the main function.
} else {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/docgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ import (

func main() {
if len(os.Args) != 2 {
log.Fatal("usage: go run cmd/docgen/main.go <output directory>")
log.Fatal("usage: go run cmd/docgen/main.go <output directory>") //nolint:forbidigo // this is OK inside the main function.
}

output := os.Args[1]
_, err := os.Stat(output)
if os.IsNotExist(err) {
err = os.Mkdir(output, 0755)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
} else if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}

err = doc.GenMarkdownTreeCustom(cmd.RootCmd, output, frontmatter, link)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
}

Expand Down
21 changes: 13 additions & 8 deletions cmd/rad/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strings"

Expand Down Expand Up @@ -109,15 +108,19 @@ func prettyPrintJSON(o any) (string, error) {
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
// It also initializes the tracerprovider for cli.
func Execute() {
//
// Execute returns true
func Execute() error {
ctx := context.WithValue(context.Background(), ConfigHolderKey, ConfigHolder)

shutdown, err := trace.InitTracer(trace.Options{
ServiceName: serviceName,
})
if err != nil {
log.Fatal(err)
fmt.Println("Error:", err)
return err
}

defer func() {
_ = shutdown(ctx)
}()
Expand All @@ -130,12 +133,14 @@ func Execute() {
if errors.Is(&cli.FriendlyError{}, err) {
fmt.Println(err.Error())
fmt.Println("\nTraceId: ", span.SpanContext().TraceID().String())
os.Exit(1)
return err
} else if err != nil {
fmt.Println("Error:", prettyPrintRPError(err))
fmt.Println("\nTraceId: ", span.SpanContext().TraceID().String())
os.Exit(1)
return err
}

return nil
}

func init() {
Expand Down Expand Up @@ -262,21 +267,21 @@ func initConfig() {
v, err := cli.LoadConfig(ConfigHolder.ConfigFilePath)
if err != nil {
fmt.Printf("Error: failed to load config: %v\n", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // this is OK inside the CLI startup.
}

ConfigHolder.Config = v

wd, err := os.Getwd()
if err != nil {
fmt.Printf("Error: failed to find current working directory: %v\n", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // this is OK inside the CLI startup.
}

dc, err := config.LoadDirectoryConfig(wd)
if err != nil {
fmt.Printf("Error: failed to load config: %v\n", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // this is OK inside the CLI startup.
}

ConfigHolder.DirectoryConfig = dc
Expand Down
7 changes: 6 additions & 1 deletion cmd/rad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
package main

import (
"os"

"github.com/project-radius/radius/cmd/rad/cmd"
)

func main() {
cmd.Execute()
err := cmd.Execute()
if err != nil {
os.Exit(1) //nolint:forbidigo // this is OK inside the main function.
}
Copy link
Contributor Author

@rynowak rynowak Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the bug in the previous version that broke the build. It was if err == nil { os.Exit(1) }. Oops

}
8 changes: 4 additions & 4 deletions cmd/ucpd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var rootCmd = &cobra.Command{

logger, flush, err := ucplog.NewLogger(ucplog.LoggerName, &options.LoggingOptions)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
defer flush()

Expand All @@ -58,11 +58,11 @@ var rootCmd = &cobra.Command{
options.TracerProviderOptions.ServiceName = server.ServiceName
shutdown, err := trace.InitTracer(options.TracerProviderOptions)
if err != nil {
log.Fatal(err)
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
defer func() {
if err := shutdown(ctx); err != nil {
log.Fatal("failed to shutdown TracerProvider: %w", err)
log.Printf("failed to shutdown TracerProvider: %v\n", err)
}
}()

Expand All @@ -87,7 +87,7 @@ var rootCmd = &cobra.Command{
// gracefully, so just crash if that happens.
err = <-stopped
if err == nil {
os.Exit(0)
os.Exit(0) //nolint:forbidigo // this is OK inside the main function.
} else {
panic(err)
}
Expand Down
Loading