Application Entry Point#
In Go, the main()
function is the entry point of the binary application and is defined in the main.go
1 file of our application:
package main
var (
version = "0.0.0"
commit = ""
)
func main() {
versionFlag := flag.Bool("version", false, "print the version and exit")
flag.Parse()
if *versionFlag {
fmt.Println(version + "-" + commit)
os.Exit(0)
}
logger := setupLogger()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
opts := []bond.Option{
bond.WithLogger(&logger),
bond.WithContext(ctx, cancel),
bond.WithAppRootPath(greeter.AppRoot),
}
agent, errs := bond.NewAgent(greeter.AppName, opts...)
for _, err := range errs {
if err != nil {
logger.Fatal().Err(err).Msg("Failed to create agent")
}
}
err := agent.Start()
if err != nil {
logger.Fatal().Err(err).Msg("Failed to start agent")
}
app := greeter.New(&logger, agent)
app.Start(ctx)
}
Application version#
As you can see, the main
function is rather simple. First, we handle the version
CLI flag to make sure our application can return its version when asked.
Application config has a version-command
field that indicates which command needs to be executed to get the application version. In our case, the version
field is set to greeter --version
and we just went through the handler of this flag.
In SR Linux CLI we can get the version of the greeter
app by executing the show system application greeter
command:
--{ + running }--[ ]--
A:greeter# show system application greeter
+---------+------+---------+-------------+--------------------------+
| Name | PID | State | Version | Last Change |
+=========+======+=========+=============+==========================+
| greeter | 4676 | running | dev-a6f880b | 2023-11-29T21:29:04.243Z |
+---------+------+---------+-------------+--------------------------+
Why the version is dev-a6f880b
?
Attentive readers might have noticed that the version of the greeter
app is dev-a6f880b
instead of v0.0.0-
following the version
and commit
variables values in main.go
file. This is because we set the values for these variables at build time using the Go linker flags in the run.sh
script:
These variables are then set to the correct values when we build the application with Goreleaser.
Setting up the Logger#
Logging is an important part of any application. It aids the developer in debugging the application and provides valuable information about the application's state for its users.
We create the logger before initializing the application so that we can pass it to the application and use it to log the application's state.
Logging from the NDK application is a separate topic that is covered in the Logging section of this guide.
Context#
Moving down the main
function, we create the context that will control the lifecycle of our greeter application.
And with the context piece out of the way, we are standing in front of the actual NDK application machinery. Let's turn the page and start digging into it.
-
Imports are omitted for brevity. ↩