github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/util/fs/files/hostname.go (about) 1 // Copyright (c) 2018, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package files 7 8 import ( 9 "fmt" 10 "regexp" 11 12 "github.com/sylabs/singularity/internal/pkg/sylog" 13 ) 14 15 var hostRegex = `^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$` 16 17 // Hostname creates a hostname content with provided hostname and returns it 18 func Hostname(hostname string) (content []byte, err error) { 19 sylog.Verbosef("Creating hostname content\n") 20 if hostname == "" { 21 return content, fmt.Errorf("no hostname provided") 22 } 23 r := regexp.MustCompile(hostRegex) 24 if !r.MatchString(hostname) { 25 return content, fmt.Errorf("%s is not a valid hostname", hostname) 26 } 27 line := fmt.Sprintf("%s\n", hostname) 28 content = append(content, line...) 29 return content, nil 30 }