github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/seed/internal/validate.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package internal
    21  
    22  import (
    23  	"fmt"
    24  	"regexp"
    25  )
    26  
    27  // validSeedSystemLabel is the regex describing a valid system label. Typically
    28  // system labels are expected to be date based, eg. 20201116, but for
    29  // completeness follow the same rule as model names (incl. one letter model
    30  // names and thus system labels), with the exception that uppercase letters are
    31  // not allowed, as the systems will often be stored in a FAT filesystem.
    32  var validSeedSystemLabel = regexp.MustCompile("^[a-z0-9](?:-?[a-z0-9])*$")
    33  
    34  // ValidateSeedSystemLabel checks whether the string is a valid UC20 seed system
    35  // label.
    36  func ValidateUC20SeedSystemLabel(label string) error {
    37  	if !validSeedSystemLabel.MatchString(label) {
    38  		return fmt.Errorf("invalid seed system label: %q", label)
    39  	}
    40  	return nil
    41  }