You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.8 KiB

  1. /*
  2. Copyright © 2021 Drew Short <warrick@sothr.com>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "os"
  16. "path"
  17. log "github.com/sirupsen/logrus"
  18. "github.com/spf13/cobra"
  19. "sothr.com/warricksothr/pinned-package-updater/internal/parser"
  20. "sothr.com/warricksothr/pinned-package-updater/internal/parser/dockerfile"
  21. )
  22. // checkCmd represents the check command
  23. var checkCmd = &cobra.Command{
  24. Use: "check",
  25. Short: "Check for updates to pinned packages in a alpine.Dockerfile",
  26. Long: `Read a alpine.Dockerfile, identify the repositories and packages it references, and locate required
  27. updates to satisfy the requirement of running the latest security version for the pinned packages.
  28. To run in standalone mode, provide a alpine.Dockerfile path to evaluate or rely on the default
  29. behavior which looks for a alpine.Dockerfile in the current directory.
  30. pinned-package-updater check [Dockerfile, ...]
  31. To run in distributed mode, provide the remote address of the service that will
  32. provide information about the remote.
  33. pinned-package-updater --remote <address of the upstream service> check [Dockerfile, ...]
  34. `,
  35. Run: func(cmd *cobra.Command, args []string) {
  36. // Handle no Dockerfile path provided
  37. if len(args) < 1 {
  38. currentDirectory, currentDirectoryErr := os.Getwd()
  39. cobra.CheckErr(currentDirectoryErr)
  40. defaultDockerfilePath := path.Join(currentDirectory, "Dockerfile")
  41. args = append(args, defaultDockerfilePath)
  42. log.Debugf("No dockerfile path provided, attempting to use the default %s dockerfile", defaultDockerfilePath)
  43. }
  44. // Aggregate the parsed updates
  45. for _, dockerfilePath := range args {
  46. log.Debugf("Parsing %s", dockerfilePath)
  47. _, err := dockerfile.Parse(dockerfilePath)
  48. if err != nil {
  49. return
  50. }
  51. }
  52. },
  53. }
  54. func init() {
  55. rootCmd.AddCommand(checkCmd)
  56. // TODO: Add dynamic loading of maps between images and command parsers
  57. err := parser.InitCommandParsers()
  58. if err != nil {
  59. log.Errorf("Unexpected error while ")
  60. return
  61. }
  62. // Here you will define your flags and configuration settings.
  63. // Cobra supports Persistent Flags which will work for this command
  64. // and all subcommands, e.g.:
  65. // checkCmd.PersistentFlags().String("foo", "", "A help for foo")
  66. // Cobra supports local flags which will only run when this command
  67. // is called directly, e.g.:
  68. // checkCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
  69. }