|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "github.com/GitHubSecurityLab/gh-qldb/utils" |
| 15 | +) |
| 16 | + |
| 17 | +var installCmd = &cobra.Command{ |
| 18 | + Use: "install", |
| 19 | + Short: "Install a local CodeQL database in the QLDB directory", |
| 20 | + Long: `Install a local CodeQL database in the QLDB directory`, |
| 21 | + Run: func(cmd *cobra.Command, args []string) { |
| 22 | + install(nwoFlag, dbPathFlag, removeFlag) |
| 23 | + }, |
| 24 | + } |
| 25 | + |
| 26 | +func init() { |
| 27 | + rootCmd.AddCommand(installCmd) |
| 28 | + installCmd.Flags().StringVarP(&nwoFlag, "nwo", "n", "", "The NWO to associate the database to.") |
| 29 | + installCmd.Flags().StringVarP(&dbPathFlag, "database", "d", "", "The path to the database to install.") |
| 30 | + installCmd.Flags().BoolVarP(&removeFlag, "remove", "r", false, "Remove the database after installing it.") |
| 31 | + installCmd.MarkFlagRequired("nwo") |
| 32 | + installCmd.MarkFlagRequired("database") |
| 33 | +} |
| 34 | + |
| 35 | +func install(nwo string, dbPath string, remove bool) { |
| 36 | + fmt.Printf("Installing '%s' DB for '%s'\n", dbPath, nwo) |
| 37 | + |
| 38 | + // Check if the path exists |
| 39 | + fileinfo, err := os.Stat(dbPath) |
| 40 | + var zipPath string |
| 41 | + if os.IsNotExist(err) { |
| 42 | + log.Fatal(errors.New("DB path does not exist")) |
| 43 | + } |
| 44 | + if fileinfo.IsDir() { |
| 45 | + err := utils.ValidateDB(dbPath) |
| 46 | + if err != nil { |
| 47 | + fmt.Println("DB is not valid") |
| 48 | + } |
| 49 | + // Compress DB |
| 50 | + zipfilename := filepath.Join(os.TempDir(), "qldb.zip") |
| 51 | + fmt.Println("Zipping DB to", zipfilename) |
| 52 | + if err := utils.ZipDirectory(zipfilename, dbPath); err != nil { |
| 53 | + log.Fatal(err) |
| 54 | + } |
| 55 | + zipPath = zipfilename |
| 56 | + |
| 57 | + } else { |
| 58 | + // Check if the file is a zip |
| 59 | + if !strings.HasSuffix(dbPath, ".zip") { |
| 60 | + log.Fatal(errors.New("DB path is not a zip file")) |
| 61 | + } |
| 62 | + |
| 63 | + zipPath = dbPath |
| 64 | + // Unzip to temporary directory |
| 65 | + tmpdir, _ := ioutil.TempDir("", "qldb") |
| 66 | + _, err := utils.Unzip(dbPath, tmpdir) |
| 67 | + if err != nil { |
| 68 | + log.Fatal(err) |
| 69 | + } |
| 70 | + files, err := ioutil.ReadDir(tmpdir) |
| 71 | + if err != nil { |
| 72 | + log.Fatal(err) |
| 73 | + } |
| 74 | + if len(files) == 1 { |
| 75 | + tmpdir = filepath.Join(tmpdir, files[0].Name()) |
| 76 | + } |
| 77 | + err = utils.ValidateDB(tmpdir) |
| 78 | + if err != nil { |
| 79 | + fmt.Println("DB is not valid") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // read bytes from dbPath |
| 84 | + zipFile, err := os.Open(zipPath) |
| 85 | + if err != nil { |
| 86 | + log.Fatal(err) |
| 87 | + } |
| 88 | + defer zipFile.Close() |
| 89 | + zipBytes, err := ioutil.ReadAll(zipFile) |
| 90 | + if err != nil { |
| 91 | + log.Fatal(err) |
| 92 | + } |
| 93 | + commitSha, primaryLanguage, err := utils.ExtractDBInfo(zipBytes) |
| 94 | + |
| 95 | + // Destination path |
| 96 | + dir := filepath.Join(utils.GetPath(nwo), primaryLanguage) |
| 97 | + filename := fmt.Sprintf("%s.zip", commitSha) |
| 98 | + path := filepath.Join(dir, filename) |
| 99 | + fmt.Println("Installing DB to", path) |
| 100 | + |
| 101 | + // Check if the DB is already installed |
| 102 | + if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { |
| 103 | + // Copy DB to the right place |
| 104 | + srcFile, err := os.Open(zipPath) |
| 105 | + if err != nil { |
| 106 | + log.Fatal(err) |
| 107 | + } |
| 108 | + defer srcFile.Close() |
| 109 | + err = os.MkdirAll(filepath.Dir(path), 0755) |
| 110 | + if err != nil { |
| 111 | + log.Fatal(err) |
| 112 | + } |
| 113 | + destFile, err := os.Create(path) |
| 114 | + if err != nil { |
| 115 | + log.Fatal(err) |
| 116 | + } |
| 117 | + defer destFile.Close() |
| 118 | + fmt.Println("Copying DB to", path) |
| 119 | + _, err = io.Copy(srcFile, destFile) // check first var for number of bytes copied |
| 120 | + if err != nil { |
| 121 | + log.Fatal(err) |
| 122 | + } |
| 123 | + err = destFile.Sync() |
| 124 | + if err != nil { |
| 125 | + log.Fatal(err) |
| 126 | + } |
| 127 | + } |
| 128 | + // Remove DB from the current location if -r flag is set |
| 129 | + if remove { |
| 130 | + fmt.Println("Removing DB from", dbPath) |
| 131 | + os.Remove(dbPath) |
| 132 | + } |
| 133 | +} |
0 commit comments