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.

51 lines
1.6 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 dockerfile implements the required components for working with dockerfile image definitions
  14. package dockerfile
  15. import (
  16. "os"
  17. log "github.com/sirupsen/logrus"
  18. buildkitDockerfile "github.com/moby/buildkit/frontend/dockerfile/parser"
  19. "sothr.com/warricksothr/pinned-package-updater/internal/parser"
  20. )
  21. // Parse reads a Dockerfile, interprets the AST and returns information about the image(s) and package(s) defined
  22. func Parse(dockerfilePath string) (parser.ParseResult, error) {
  23. dockerfile, err := os.Open(dockerfilePath)
  24. if err != nil {
  25. log.Fatalf("Unexpected error reading file %s: %s", dockerfilePath, err)
  26. return parser.ParseResult{}, err
  27. }
  28. defer func(dockerfile *os.File) {
  29. err := dockerfile.Close()
  30. if err != nil {
  31. log.Error("Unexpected error closing file %s: %s", dockerfilePath, err)
  32. }
  33. }(dockerfile)
  34. _, err = buildkitDockerfile.Parse(dockerfile)
  35. if err != nil {
  36. log.Fatalf("Unexpected error parsing Dockerfile %s: %s", dockerfilePath, err)
  37. return parser.ParseResult{}, err
  38. }
  39. return parser.ParseResult{}, nil
  40. }