示例#1
0
func (g *Group) ensureAbsent(result *task.Result) (*task.Result, error) {
	var err error
	oldGroup, err := unixgroup.Lookup(g.Name, nil)
	if err != nil {
		if _, ok := err.(unixgroup.UnknownGroupError); ok {
			err = nil
			result.Skipped = true
		}
		return result, err
	}

	result.Extra["old_gid"], err = strconv.Atoi(oldGroup.Gid)
	if err != nil {
		return result, err
	}

	err = result.ExecCommand("groupdel", g.Name)
	return result, err
}
示例#2
0
func (g *Group) ensurePresent(result *task.Result) (*task.Result, error) {
	var gidStr string
	if g.Gid != AutoGID {
		gidStr = strconv.Itoa(g.Gid)
	}

	var command string
	var err error
	oldGroup, err := unixgroup.Lookup(g.Name, nil)
	if err != nil {
		if _, ok := err.(unixgroup.UnknownGroupError); !ok {
			return result, err
		}

		result.Op = "create"
		command = "groupadd"
	} else {
		if gidStr == "" || gidStr == oldGroup.Gid {
			result.Skipped = true
			return result, err
		}

		result.Extra["old_gid"], err = strconv.Atoi(oldGroup.Gid)
		if err != nil {
			return result, err
		}

		result.Op = "modify"
		command = "groupmod"
	}

	args := make([]string, 0)
	if gidStr != "" {
		args = append(args, "-g", gidStr)
	}
	if g.System {
		args = append(args, "-r")
	}
	args = append(args, g.Name)

	err = result.ExecCommand(command, args...)
	return result, err
}