refactor: enhancement of logging, config and metrics

This commit is contained in:
Jonas Kaninda
2024-11-11 08:50:34 +01:00
parent e25bc218b5
commit 11c72e5e17
12 changed files with 97 additions and 65 deletions

View File

@@ -18,9 +18,10 @@
package config
import (
"fmt"
pkg "github.com/jkaninda/goma-gateway/internal"
"github.com/spf13/cobra"
"log"
"os"
)
var CheckConfigCmd = &cobra.Command{
@@ -29,13 +30,15 @@ var CheckConfigCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
configFile, _ := cmd.Flags().GetString("config")
if configFile == "" {
log.Fatalln("no config file specified")
fmt.Println("no config file specified")
os.Exit(1)
}
err := pkg.CheckConfig(configFile)
if err != nil {
log.Fatalf(" Error checking config file: %s\n", err)
fmt.Printf(" Error checking config file: %s\n", err)
os.Exit(1)
}
log.Println("Goma Gateway configuration file checked successfully")
fmt.Println("Goma Gateway configuration file checked successfully")
},
}

View File

@@ -17,8 +17,9 @@ limitations under the License.
package config
import (
"fmt"
"github.com/spf13/cobra"
"log"
"os"
)
var Cmd = &cobra.Command{
@@ -28,8 +29,8 @@ var Cmd = &cobra.Command{
if len(args) == 0 {
return
} else {
log.Fatalf("Config accepts no argument %q", args)
fmt.Printf("config accepts no argument %q\n", args)
os.Exit(1)
}
},

View File

@@ -16,9 +16,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"fmt"
"github.com/jkaninda/goma-gateway/internal"
"github.com/jkaninda/goma-gateway/pkg/logger"
"github.com/spf13/cobra"
"os"
)
var InitConfigCmd = &cobra.Command{
@@ -26,14 +27,34 @@ var InitConfigCmd = &cobra.Command{
Short: "Initialize Goma Gateway configuration file",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
pkg.InitConfig(cmd)
force, _ := cmd.Flags().GetBool("force")
configFile, _ := cmd.Flags().GetString("output")
if configFile == "" {
fmt.Println("Error: no config file specified")
os.Exit(1)
}
// Check if the config file exists
if _, err := os.Stat(configFile); !os.IsNotExist(err) {
if !force {
fmt.Printf("%s config file already exists, use -f to overwrite\n", configFile)
os.Exit(1)
}
}
err := pkg.InitConfig(configFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("configuration file has been initialized successfully")
} else {
logger.Fatal(`"config" accepts no argument %q`, args)
fmt.Printf("config accepts no argument %q\n", args)
os.Exit(1)
}
},
}
func init() {
InitConfigCmd.Flags().StringP("output", "o", "", "config file output")
InitConfigCmd.Flags().StringP("output", "o", "", "configuration file output")
InitConfigCmd.Flags().BoolP("force", "f", false, "Force overwrite configuration file")
}