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.

116 lines
3.3 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 parser implements the required components for working with image definitions
  14. package parser
  15. import (
  16. "errors"
  17. "fmt"
  18. log "github.com/sirupsen/logrus"
  19. )
  20. // ParseResult is the standard representation for am image declaration
  21. type ParseResult struct {
  22. Stages []*Stage
  23. }
  24. // Stage is the representation of a stage in the image declaration
  25. type Stage struct {
  26. Name string
  27. Image *Image
  28. Repositories []*Repository
  29. Packages []*Package
  30. }
  31. // Image is the representation of the reference to a container image layer
  32. type Image struct {
  33. Name string
  34. Tag string
  35. }
  36. // Package is the representation of an installed pinned package defined in an image stage declaration
  37. type Package struct {
  38. Name string
  39. Version string
  40. }
  41. // Repository is the representation of an added repository defined in a stage declaration
  42. type Repository struct {
  43. Information string
  44. }
  45. // CommandParserType represents the recognized type of parser usable for a specific image
  46. type CommandParserType int
  47. const (
  48. CommandParserTypeDebian CommandParserType = iota
  49. )
  50. // CommandParserTypeIds is a lookup map between CommandParserType and a representation string
  51. var commandParserTypeIds = map[CommandParserType][]string{
  52. CommandParserTypeDebian: {"debian"},
  53. }
  54. func lookupCommandParserTypeName(parserType CommandParserType) string {
  55. if parserTypeName, ok := commandParserTypeIds[parserType]; ok {
  56. return parserTypeName[0]
  57. } else {
  58. return "UNKNOWN"
  59. }
  60. }
  61. var availableCommandParsers = map[CommandParserType]CommandParser{}
  62. // CommandParser handles parsing commands from RUN arguments dependent on the base image type
  63. type CommandParser interface {
  64. GetRepositories(runCommand string) ([]*Repository, error)
  65. GetPinnedPackages(runCommand string) ([]*Package, error)
  66. }
  67. // InitCommandParsers exposes functionality to configure the behavior of mapping images to a CommandParser
  68. func InitCommandParsers() error {
  69. // Registering known parsers
  70. availableCommandParsers[CommandParserTypeDebian] = newDebianCommandParser()
  71. return nil
  72. }
  73. func GetCommandParser(image Image) (*CommandParser, error) {
  74. parserType, err := determineParserForImage(image)
  75. if err != nil {
  76. log.Fatalf("Failed to determine a matching CommandParser for %s: %s", image, err)
  77. return nil, err
  78. }
  79. if requestedCommandParser, ok := availableCommandParsers[parserType]; ok {
  80. return &requestedCommandParser, nil
  81. } else {
  82. message := fmt.Sprintf("Unable to identify parser for %s", lookupCommandParserTypeName(parserType))
  83. log.Fatal(message)
  84. return nil, errors.New(message)
  85. }
  86. }
  87. // determineParserForImage matches image information to the correct CommandParserType
  88. func determineParserForImage(_ Image) (CommandParserType, error) {
  89. // TODO: Map image information to the list of command parsers supported
  90. return CommandParserTypeDebian, nil
  91. }