/* Copyright © 2021 Drew Short Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // Package parser implements the required components for working with image definitions package parser import ( "errors" "fmt" log "github.com/sirupsen/logrus" ) // ParseResult is the standard representation for am image declaration type ParseResult struct { Stages []*Stage } // Stage is the representation of a stage in the image declaration type Stage struct { Name string Image *Image Repositories []*Repository Packages []*Package } // Image is the representation of the reference to a container image layer type Image struct { Name string Tag string } // Package is the representation of an installed pinned package defined in an image stage declaration type Package struct { Name string Version string } // Repository is the representation of an added repository defined in a stage declaration type Repository struct { Information string } // CommandParserType represents the recognized type of parser usable for a specific image type CommandParserType int const ( CommandParserTypeDebian CommandParserType = iota ) // CommandParserTypeIds is a lookup map between CommandParserType and a representation string var commandParserTypeIds = map[CommandParserType][]string{ CommandParserTypeDebian: {"debian"}, } func lookupCommandParserTypeName(parserType CommandParserType) string { if parserTypeName, ok := commandParserTypeIds[parserType]; ok { return parserTypeName[0] } else { return "UNKNOWN" } } var availableCommandParsers = map[CommandParserType]CommandParser{} // CommandParser handles parsing commands from RUN arguments dependent on the base image type type CommandParser interface { GetRepositories(runCommand string) ([]*Repository, error) GetPinnedPackages(runCommand string) ([]*Package, error) } // InitCommandParsers exposes functionality to configure the behavior of mapping images to a CommandParser func InitCommandParsers() error { // Registering known parsers availableCommandParsers[CommandParserTypeDebian] = newDebianCommandParser() return nil } func GetCommandParser(image Image) (*CommandParser, error) { parserType, err := determineParserForImage(image) if err != nil { log.Fatalf("Failed to determine a matching CommandParser for %s: %s", image, err) return nil, err } if requestedCommandParser, ok := availableCommandParsers[parserType]; ok { return &requestedCommandParser, nil } else { message := fmt.Sprintf("Unable to identify parser for %s", lookupCommandParserTypeName(parserType)) log.Fatal(message) return nil, errors.New(message) } } // determineParserForImage matches image information to the correct CommandParserType func determineParserForImage(_ Image) (CommandParserType, error) { // TODO: Map image information to the list of command parsers supported return CommandParserTypeDebian, nil }