11package cmd
22
33import (
4+ "bufio"
45 "encoding/json"
56 "fmt"
67 "os"
7- "path/filepath"
88 "strings"
99
1010 "github.com/metal-stack/sonic-configdb-utils/configdb"
11- "github.com/metal-stack/sonic-configdb-utils/platform"
11+ p "github.com/metal-stack/sonic-configdb-utils/platform"
1212 "github.com/metal-stack/sonic-configdb-utils/values"
1313 "github.com/spf13/cobra"
1414)
@@ -17,66 +17,62 @@ var generateCmd = &cobra.Command{
1717 Use : "generate" ,
1818 Short : "Generate a config_db.json" ,
1919 Run : func (cmd * cobra.Command , args []string ) {
20- platformFile , _ := cmd .Flags ().GetString ("platform-file" )
21- if platformFile == "" {
22- fmt .Println ("missing platform.json file; please provide a platform.json file via --platform-file flag" )
23- os .Exit (1 )
20+ configDir , _ := cmd .Flags ().GetString ("sonic-config-dir" )
21+
22+ sonicPlatform , _ := cmd .Flags ().GetString ("platform" )
23+ if sonicPlatform == "" {
24+ var err error
25+ sonicPlatform , err = retrieveSonicPlatform (configDir )
26+ if err != nil {
27+ fmt .Printf ("failed to retrieve sonic platform: %v\n " , err )
28+ os .Exit (1 )
29+ }
2430 }
2531
26- bytes , err := os .ReadFile (platformFile )
27- if err != nil {
28- fmt .Printf ("failed to read platform.json file, %v\n " , err )
29- os .Exit (1 )
30- }
32+ deviceDir , _ := cmd .Flags ().GetString ("device-dir" )
33+ platformFile := fmt .Sprintf ("%s/%s/platform.json" , deviceDir , sonicPlatform )
3134
32- platform , err := platform . UnmarshalPlatformJSON ( bytes )
35+ platformBytes , err := os . ReadFile ( platformFile )
3336 if err != nil {
34- fmt .Printf ("failed to parse platform.json, %v\n " , err )
37+ fmt .Printf ("failed to read platform.json file: %v\n " , err )
3538 os .Exit (1 )
3639 }
3740
38- inputFile , _ := cmd . Flags (). GetString ( "input" )
39- if inputFile == "" {
40- fmt .Println ( "missing input values; please provide an input file via --input flag" )
41+ platform , err := p . UnmarshalPlatformJSON ( platformBytes )
42+ if err != nil {
43+ fmt .Printf ( "failed to parse platform.json: %v \n " , err )
4144 os .Exit (1 )
4245 }
4346
44- bytes , err = os .ReadFile (inputFile )
47+ inputFile , _ := cmd .Flags ().GetString ("input-file" )
48+ inputBytes , err := os .ReadFile (inputFile )
4549 if err != nil {
4650 fmt .Printf ("failed to read input file, %v\n " , err )
4751 os .Exit (1 )
4852 }
4953
50- values , err := values .UnmarshalValues (bytes )
54+ values , err := values .UnmarshalValues (inputBytes )
5155 if err != nil {
5256 fmt .Printf ("failed to parse input file, %v\n " , err )
5357 os .Exit (1 )
5458 }
5559
56- config , err := configdb .GenerateConfigDB (values , platform )
60+ configDB , err := configdb .GenerateConfigDB (values , platform )
5761 if err != nil {
5862 fmt .Printf ("failed to generate config, %v\n " , err )
5963 os .Exit (1 )
6064 }
6165
62- bytes , err = json .MarshalIndent (config , "" , " " )
66+ configBytes , err : = json .MarshalIndent (configDB , "" , " " )
6367 if err != nil {
6468 fmt .Printf ("failed to serialize json, %v\n " , err )
6569 os .Exit (1 )
6670 }
6771
68- fileInfo , err := os .Lstat (inputFile )
69- if err != nil {
70- fmt .Printf ("failed to retrieve file info for %s, %v\n " , inputFile , err )
71- os .Exit (1 )
72- }
73-
74- outputFile , _ := cmd .Flags ().GetString ("output" )
75- if outputFile == "" {
76- outputFile = filenameWithoutExtension (inputFile ) + ".json"
77- }
72+ outputFileName , _ := cmd .Flags ().GetString ("output-file-name" )
73+ outputFile := fmt .Sprintf ("%s/%s" , configDir , outputFileName )
7874
79- err = os .WriteFile (outputFile , bytes , fileInfo . Mode () )
75+ err = os .WriteFile (outputFile , configBytes , 0644 )
8076 if err != nil {
8177 fmt .Printf ("failed to write file, %v" , err )
8278 os .Exit (1 )
@@ -87,11 +83,28 @@ var generateCmd = &cobra.Command{
8783func init () {
8884 rootCmd .AddCommand (generateCmd )
8985
90- generateCmd .Flags ().StringP ("input" , "i" , "" , "input file to generate the config_db.json from" )
91- generateCmd .Flags ().StringP ("platform-file" , "f" , "" , "path to a vendor-specific platform.json file" )
92- generateCmd .Flags ().StringP ("output" , "o" , "" , "output file" )
86+ generateCmd .Flags ().String ("device-dir" , "/usr/share/sonic/device" , "directory that holds all vendor specific files" )
87+ generateCmd .Flags ().StringP ("input-file" , "i" , "sonic-config.yaml" , "path to input file to generate the config_db.json from" )
88+ generateCmd .Flags ().StringP ("output-file-name" , "o" , "config_db.json" , "output file name" )
89+ generateCmd .Flags ().StringP ("platform" , "p" , "" , "sonic platform" )
90+ generateCmd .Flags ().String ("sonic-config-dir" , "/etc/sonic" , "where to store the generated config_db.json" )
9391}
9492
95- func filenameWithoutExtension (name string ) string {
96- return strings .TrimSuffix (filepath .Base (name ), filepath .Ext (name ))
93+ func retrieveSonicPlatform (configDir string ) (string , error ) {
94+ sonicEnvFile := fmt .Sprintf ("%s/sonic-environment" , configDir )
95+ f , err := os .Open (sonicEnvFile )
96+ if err != nil {
97+ return "" , err
98+ }
99+ defer f .Close ()
100+
101+ scanner := bufio .NewScanner (f )
102+ for scanner .Scan () {
103+ if line := scanner .Text (); strings .Contains (line , "PLATFORM=" ) {
104+ sonicPlatform , _ := strings .CutPrefix (line , "PLATFORM=" )
105+ return sonicPlatform , nil
106+ }
107+ }
108+
109+ return "" , fmt .Errorf ("no platform information found" )
97110}
0 commit comments