pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/knf/validators/system/validators.go (about)

     1  // Package system provides KNF validators for checking system items (user, groups,
     2  // network interfaces)
     3  package system
     4  
     5  // ////////////////////////////////////////////////////////////////////////////////// //
     6  //                                                                                    //
     7  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     8  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     9  //                                                                                    //
    10  // ////////////////////////////////////////////////////////////////////////////////// //
    11  
    12  import (
    13  	"fmt"
    14  
    15  	"pkg.re/essentialkaos/ek.v12/knf"
    16  	"pkg.re/essentialkaos/ek.v12/system"
    17  )
    18  
    19  // ////////////////////////////////////////////////////////////////////////////////// //
    20  
    21  var (
    22  	// User returns error if config property contains name of user or UID which not
    23  	// present on the system
    24  	User = validateUser
    25  
    26  	// Group returns error if config property contains name of group or GID which not
    27  	// present on the system
    28  	Group = validateGroup
    29  )
    30  
    31  // ////////////////////////////////////////////////////////////////////////////////// //
    32  
    33  func validateUser(config *knf.Config, prop string, value interface{}) error {
    34  	userNameOrID := config.GetS(prop)
    35  
    36  	if userNameOrID == "" {
    37  		return nil
    38  	}
    39  
    40  	if !system.IsUserExist(userNameOrID) {
    41  		return fmt.Errorf("User %s is not present on the system", userNameOrID)
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  func validateGroup(config *knf.Config, prop string, value interface{}) error {
    48  	groupNameOrID := config.GetS(prop)
    49  
    50  	if groupNameOrID == "" {
    51  		return nil
    52  	}
    53  
    54  	if !system.IsGroupExist(groupNameOrID) {
    55  		return fmt.Errorf("Group %s is not present on the system", groupNameOrID)
    56  	}
    57  
    58  	return nil
    59  }