@@ -30,7 +30,8 @@ const (
3030)
3131
3232var (
33- configFilePath string
33+ configFilePath string
34+ sessionsFilePath string
3435)
3536
3637func runCodeQLCommand (codeqlPath string , combined bool , args ... string ) ([]byte , error ) {
@@ -476,19 +477,19 @@ func downloadDatabase(nwo string, language string, outputDir string) error {
476477 return nil
477478}
478479
479- func saveInHistory (name string , controller string , runs []Run , language string , listFile string , list string , query string , count int ) error {
480- configData , err := getConfig ( configFilePath )
480+ func saveSession (name string , controller string , runs []Run , language string , listFile string , list string , query string , count int ) error {
481+ sessions , err := getSessions ( )
481482 if err != nil {
482483 return err
483484 }
484- if configData . History == nil {
485- configData . History = make (map [string ]HistoryEntry )
485+ if sessions == nil {
486+ sessions = make (map [string ]Session )
486487 }
487- // add new history entry if it doesn't already exist
488- if _ , ok := configData . History [name ]; ok {
489- return errors .New ("Name already exists in history" )
488+ // add new session if it doesn't already exist
489+ if _ , ok := sessions [name ]; ok {
490+ return errors .New (fmt . Sprintf ( "Session '%s' already exists" , name ) )
490491 } else {
491- configData . History [name ] = HistoryEntry {
492+ sessions [name ] = Session {
492493 Name : name ,
493494 Runs : runs ,
494495 Timestamp : time .Now (),
@@ -499,33 +500,46 @@ func saveInHistory(name string, controller string, runs []Run, language string,
499500 RepositoryCount : count ,
500501 }
501502 }
502- // marshal config data to yaml
503- configDataYaml , err := yaml .Marshal (configData )
503+ // marshal sessions to yaml
504+ sessionsYaml , err := yaml .Marshal (sessions )
504505 if err != nil {
505506 return err
506507 }
507- // write config data to file
508- err = ioutil .WriteFile (configFilePath , configDataYaml , os .ModePerm )
508+ // write sessions to file
509+ err = ioutil .WriteFile (sessionsFilePath , sessionsYaml , os .ModePerm )
509510 if err != nil {
510511 return err
511512 }
512513 return nil
513514}
514515
515- func loadFromHistory (name string ) (string , []Run , string , error ) {
516- configData , err := getConfig ( configFilePath )
516+ func loadSession (name string ) (string , []Run , string , error ) {
517+ sessions , err := getSessions ( )
517518 if err != nil {
518519 return "" , nil , "" , err
519520 }
520- if configData . History != nil {
521- if entry , ok := configData . History [name ]; ok {
521+ if sessions != nil {
522+ if entry , ok := sessions [name ]; ok {
522523 return entry .Controller , entry .Runs , entry .Language , nil
523524 }
524525 }
525- return "" , nil , "" , errors .New ("No history entry found for " + name )
526+ return "" , nil , "" , errors .New ("No session found for " + name )
526527}
527528
528- func getConfig (path string ) (Config , error ) {
529+ func getSessions () (map [string ]Session , error ) {
530+ sessionsFile , err := ioutil .ReadFile (sessionsFilePath )
531+ var sessions map [string ]Session
532+ if err != nil {
533+ return sessions , err
534+ }
535+ err = yaml .Unmarshal (sessionsFile , & sessions )
536+ if err != nil {
537+ log .Fatal (err )
538+ }
539+ return sessions , nil
540+ }
541+
542+ func getConfig () (Config , error ) {
529543 configFile , err := ioutil .ReadFile (configFilePath )
530544 var configData Config
531545 if err != nil {
@@ -539,25 +553,24 @@ func getConfig(path string) (Config, error) {
539553}
540554
541555type Run struct {
542- Id int `yaml:"run_id "`
556+ Id int `yaml:"id "`
543557 Query string `yaml:"query"`
544558}
545559
546- type HistoryEntry struct {
560+ type Session struct {
547561 Name string `yaml:"name"`
548562 Timestamp time.Time `yaml:"timestamp"`
549- Runs []Run `yaml:"runIds "`
563+ Runs []Run `yaml:"runs "`
550564 Controller string `yaml:"controller"`
551- ListFile string `yaml:"listFile "`
565+ ListFile string `yaml:"list_file "`
552566 List string `yaml:"list"`
553567 Language string `yaml:"language"`
554- RepositoryCount int `yaml:"repositoryCount "`
568+ RepositoryCount int `yaml:"repository_count "`
555569}
556570type Config struct {
557- Controller string `yaml:"controller"`
558- ListFile string `yaml:"listFile"`
559- CodeQLPath string `yaml:"codeqlPath"`
560- History map [string ]HistoryEntry `yaml:"history"`
571+ Controller string `yaml:"controller"`
572+ ListFile string `yaml:"list_file"`
573+ CodeQLPath string `yaml:"codeql_path"`
561574}
562575
563576func main () {
@@ -570,25 +583,24 @@ func main() {
570583 configPath = filepath .Join (homePath , ".config" )
571584 }
572585 configFilePath = filepath .Join (configPath , "gh-mrva" , "config.yml" )
573- if _ , err := os .Stat (configFilePath ); os .IsNotExist (err ) {
574- // create config file if it doesn't exist
575- // since we will use it for storing the history
576- err := os .MkdirAll (filepath .Dir (configFilePath ), os .ModePerm )
586+ configData , err := getConfig ()
587+ if err != nil {
588+ log .Fatal (err )
589+ }
590+
591+ sessionsFilePath = filepath .Join (configPath , "gh-mrva" , "sessions.yml" )
592+ if _ , err := os .Stat (sessionsFilePath ); os .IsNotExist (err ) {
593+ err := os .MkdirAll (filepath .Dir (sessionsFilePath ), os .ModePerm )
577594 if err != nil {
578- log .Println ("Failed to create config file directory" )
595+ log .Fatal ("Failed to create config directory" )
579596 }
580- // create empty file at configFilePath
581- configFile , err := os .Create (configFilePath )
597+ // create empty file at sessionsFilePath
598+ sessionsFile , err := os .Create (sessionsFilePath )
582599 if err != nil {
583- log .Fatal (err , "Failed to create config file" )
600+ log .Fatal ("Failed to create sessions file" )
584601 }
585- configFile .Close ()
602+ sessionsFile .Close ()
586603 }
587- configData , err := getConfig (configFilePath )
588- if err != nil {
589- log .Fatal (err )
590- }
591-
592604 helpFlag := flag .String ("help" , "" , "This help documentation." )
593605
594606 flag .Usage = func () {
@@ -663,7 +675,7 @@ Usage:
663675 os .Exit (1 )
664676 }
665677
666- controller , runs , _ , err := loadFromHistory (runName )
678+ controller , runs , _ , err := loadSession (runName )
667679 if err != nil {
668680 log .Fatal (err )
669681 }
@@ -897,9 +909,9 @@ Usage:
897909
898910 }
899911 if querySuiteFile != "" {
900- err = saveInHistory (runName , controller , runs , language , listFile , list , querySuiteFile , len (repositories ))
912+ err = saveSession (runName , controller , runs , language , listFile , list , querySuiteFile , len (repositories ))
901913 } else if queryFile != "" {
902- err = saveInHistory (runName , controller , runs , language , listFile , list , queryFile , len (repositories ))
914+ err = saveSession (runName , controller , runs , language , listFile , list , queryFile , len (repositories ))
903915 }
904916 if err != nil {
905917 log .Fatal (err )
@@ -926,13 +938,13 @@ Usage:
926938
927939 var jsonOutput = * jsonFlag
928940
929- configData , err := getConfig ( configFilePath )
941+ sessions , err := getSessions ( )
930942 if err != nil {
931943 log .Fatal (err )
932944 }
933- if configData . History != nil {
945+ if sessions != nil {
934946 if jsonOutput {
935- for _ , entry := range configData . History {
947+ for _ , entry := range sessions {
936948 data , err := json .MarshalIndent (entry , "" , " " )
937949 if err != nil {
938950 log .Fatal (err )
@@ -943,16 +955,17 @@ Usage:
943955 // fmt.Println(w.String())
944956 }
945957 } else {
946- for name , entry := range configData . History {
958+ for name , entry := range sessions {
947959 fmt .Printf ("%s (%v)\n " , name , entry .Timestamp )
948960 fmt .Printf (" Controller: %s\n " , entry .Controller )
949961 fmt .Printf (" Language: %s\n " , entry .Language )
950962 fmt .Printf (" List file: %s\n " , entry .ListFile )
951963 fmt .Printf (" List: %s\n " , entry .List )
952964 fmt .Printf (" Repository count: %d\n " , entry .RepositoryCount )
965+ fmt .Println (" Runs:" )
953966 for _ , run := range entry .Runs {
954- fmt .Printf (" Run ID: %s \n " , run .Id )
955- fmt .Printf (" Query(s) : %s\n " , run .Query )
967+ fmt .Printf (" ID: %d \n " , run .Id )
968+ fmt .Printf (" Query : %s\n " , run .Query )
956969 }
957970 }
958971 }
@@ -1000,7 +1013,7 @@ Usage:
10001013 }
10011014 }
10021015
1003- controller , runs , language , err := loadFromHistory (runName )
1016+ controller , runs , language , err := loadSession (runName )
10041017 if err != nil {
10051018 log .Fatal (err )
10061019 } else if len (runs ) == 0 {
0 commit comments