From 35ab0f1fe6d5965943935f427024e941d6662c84 Mon Sep 17 00:00:00 2001 From: Haiyan Meng Date: Sun, 24 Jul 2016 16:18:45 -0400 Subject: [PATCH] Use Generator pointers as methods receivers Signed-off-by: Haiyan Meng --- generate/generate.go | 150 +++++++++++++++++++++---------------------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/generate/generate.go b/generate/generate.go index f979e9fc3..e189d3c66 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -170,17 +170,17 @@ func NewFromTemplate(r io.Reader) (Generator, error) { } // SetSpec sets the spec in the Generator g. -func (g Generator) SetSpec(spec *rspec.Spec) { +func (g *Generator) SetSpec(spec *rspec.Spec) { g.spec = spec } // GetSpec gets the spec in the Generator g. -func (g Generator) GetSpec() *rspec.Spec { +func (g *Generator) GetSpec() *rspec.Spec { return g.spec } // Save writes the spec into w. -func (g Generator) Save(w io.Writer) error { +func (g *Generator) Save(w io.Writer) error { data, err := json.MarshalIndent(g.spec, "", "\t") if err != nil { return err @@ -195,7 +195,7 @@ func (g Generator) Save(w io.Writer) error { } // SaveToFile writes the spec into a file. -func (g Generator) SaveToFile(path string) error { +func (g *Generator) SaveToFile(path string) error { f, err := os.Create(path) if err != nil { return err @@ -205,32 +205,32 @@ func (g Generator) SaveToFile(path string) error { } // SetVersion sets g.spec.Version. -func (g Generator) SetVersion(version string) { +func (g *Generator) SetVersion(version string) { g.spec.Version = version } // SetRootPath sets g.spec.Root.Path. -func (g Generator) SetRootPath(path string) { +func (g *Generator) SetRootPath(path string) { g.spec.Root.Path = path } // SetRootReadonly sets g.spec.Root.Readonly. -func (g Generator) SetRootReadonly(b bool) { +func (g *Generator) SetRootReadonly(b bool) { g.spec.Root.Readonly = b } // SetHostname sets g.spec.Hostname. -func (g Generator) SetHostname(s string) { +func (g *Generator) SetHostname(s string) { g.spec.Hostname = s } // ClearAnnotations clears g.spec.Annotations. -func (g Generator) ClearAnnotations() { +func (g *Generator) ClearAnnotations() { g.spec.Annotations = make(map[string]string) } // AddAnnotation adds an annotation into g.spec.Annotations. -func (g Generator) AddAnnotation(s string) error { +func (g *Generator) AddAnnotation(s string) error { if g.spec.Annotations == nil { g.spec.Annotations = make(map[string]string) } @@ -244,7 +244,7 @@ func (g Generator) AddAnnotation(s string) error { } // RemoveAnnotation remove an annotation from g.spec.Annotations. -func (g Generator) RemoveAnnotation(key string) { +func (g *Generator) RemoveAnnotation(key string) { if g.spec.Annotations == nil { return } @@ -252,67 +252,67 @@ func (g Generator) RemoveAnnotation(key string) { } // SetPlatformOS sets g.spec.Process.OS. -func (g Generator) SetPlatformOS(os string) { +func (g *Generator) SetPlatformOS(os string) { g.spec.Platform.OS = os } // SetPlatformArch sets g.spec.Platform.Arch. -func (g Generator) SetPlatformArch(arch string) { +func (g *Generator) SetPlatformArch(arch string) { g.spec.Platform.Arch = arch } // SetProcessUID sets g.spec.Process.User.UID. -func (g Generator) SetProcessUID(uid uint32) { +func (g *Generator) SetProcessUID(uid uint32) { g.spec.Process.User.UID = uid } // SetProcessGID sets g.spec.Process.User.GID. -func (g Generator) SetProcessGID(gid uint32) { +func (g *Generator) SetProcessGID(gid uint32) { g.spec.Process.User.GID = gid } // SetProcessCwd sets g.spec.Process.Cwd. -func (g Generator) SetProcessCwd(cwd string) { +func (g *Generator) SetProcessCwd(cwd string) { g.spec.Process.Cwd = cwd } // SetProcessNoNewPrivileges sets g.spec.Process.NoNewPrivileges. -func (g Generator) SetProcessNoNewPrivileges(b bool) { +func (g *Generator) SetProcessNoNewPrivileges(b bool) { g.spec.Process.NoNewPrivileges = b } // SetProcessTerminal sets g.spec.Process.Terminal. -func (g Generator) SetProcessTerminal(b bool) { +func (g *Generator) SetProcessTerminal(b bool) { g.spec.Process.Terminal = b } // SetProcessApparmorProfile sets g.spec.Process.ApparmorProfile. -func (g Generator) SetProcessApparmorProfile(prof string) { +func (g *Generator) SetProcessApparmorProfile(prof string) { g.spec.Process.ApparmorProfile = prof } // SetProcessArgs sets g.spec.Process.Args. -func (g Generator) SetProcessArgs(args []string) { +func (g *Generator) SetProcessArgs(args []string) { g.spec.Process.Args = args } // ClearProcessEnv clears g.spec.Process.Env. -func (g Generator) ClearProcessEnv() { +func (g *Generator) ClearProcessEnv() { g.spec.Process.Env = []string{} } // AddProcessEnv adds env into g.spec.Process.Env. -func (g Generator) AddProcessEnv(env string) { +func (g *Generator) AddProcessEnv(env string) { g.spec.Process.Env = append(g.spec.Process.Env, env) } // ClearProcessAdditionalGids clear g.spec.Process.AdditionalGids. -func (g Generator) ClearProcessAdditionalGids() { +func (g *Generator) ClearProcessAdditionalGids() { g.spec.Process.User.AdditionalGids = []uint32{} } // AddProcessAdditionalGid adds an additional gid into g.spec.Process.AdditionalGids. -func (g Generator) AddProcessAdditionalGid(gid string) error { +func (g *Generator) AddProcessAdditionalGid(gid string) error { groupID, err := strconv.Atoi(gid) if err != nil { return err @@ -328,92 +328,92 @@ func (g Generator) AddProcessAdditionalGid(gid string) error { } // SetProcessSelinuxLabel sets g.spec.Process.SelinuxLabel. -func (g Generator) SetProcessSelinuxLabel(label string) { +func (g *Generator) SetProcessSelinuxLabel(label string) { g.spec.Process.SelinuxLabel = label } // SetLinuxCgroupsPath sets g.spec.Linux.CgroupsPath. -func (g Generator) SetLinuxCgroupsPath(path string) { +func (g *Generator) SetLinuxCgroupsPath(path string) { g.spec.Linux.CgroupsPath = strPtr(path) } // SetLinuxMountLabel sets g.spec.Linux.MountLabel. -func (g Generator) SetLinuxMountLabel(label string) { +func (g *Generator) SetLinuxMountLabel(label string) { g.spec.Linux.MountLabel = label } // SetLinuxResourcesCPUShares sets g.spec.Linux.Resources.CPU.Shares. -func (g Generator) SetLinuxResourcesCPUShares(shares uint64) { +func (g *Generator) SetLinuxResourcesCPUShares(shares uint64) { g.spec.Linux.Resources.CPU.Shares = &shares } // SetLinuxResourcesCPUQuota sets g.spec.Linux.Resources.CPU.Quota. -func (g Generator) SetLinuxResourcesCPUQuota(quota uint64) { +func (g *Generator) SetLinuxResourcesCPUQuota(quota uint64) { g.spec.Linux.Resources.CPU.Quota = "a } // SetLinuxResourcesCPUPeriod sets g.spec.Linux.Resources.CPU.Period. -func (g Generator) SetLinuxResourcesCPUPeriod(period uint64) { +func (g *Generator) SetLinuxResourcesCPUPeriod(period uint64) { g.spec.Linux.Resources.CPU.Period = &period } // SetLinuxResourcesCPURealtimeRuntime sets g.spec.Linux.Resources.CPU.RealtimeRuntime. -func (g Generator) SetLinuxResourcesCPURealtimeRuntime(time uint64) { +func (g *Generator) SetLinuxResourcesCPURealtimeRuntime(time uint64) { g.spec.Linux.Resources.CPU.RealtimeRuntime = &time } // SetLinuxResourcesCPURealtimePeriod sets g.spec.Linux.Resources.CPU.RealtimePeriod. -func (g Generator) SetLinuxResourcesCPURealtimePeriod(period uint64) { +func (g *Generator) SetLinuxResourcesCPURealtimePeriod(period uint64) { g.spec.Linux.Resources.CPU.RealtimePeriod = &period } // SetLinuxResourcesCPUCpus sets g.spec.Linux.Resources.CPU.Cpus. -func (g Generator) SetLinuxResourcesCPUCpus(cpus string) { +func (g *Generator) SetLinuxResourcesCPUCpus(cpus string) { g.spec.Linux.Resources.CPU.Cpus = &cpus } // SetLinuxResourcesCPUMems sets g.spec.Linux.Resources.CPU.Mems. -func (g Generator) SetLinuxResourcesCPUMems(mems string) { +func (g *Generator) SetLinuxResourcesCPUMems(mems string) { g.spec.Linux.Resources.CPU.Mems = &mems } // SetLinuxResourcesMemoryLimit sets g.spec.Linux.Resources.Memory.Limit. -func (g Generator) SetLinuxResourcesMemoryLimit(limit uint64) { +func (g *Generator) SetLinuxResourcesMemoryLimit(limit uint64) { g.spec.Linux.Resources.Memory.Limit = &limit } // SetLinuxResourcesMemoryReservation sets g.spec.Linux.Resources.Memory.Reservation. -func (g Generator) SetLinuxResourcesMemoryReservation(reservation uint64) { +func (g *Generator) SetLinuxResourcesMemoryReservation(reservation uint64) { g.spec.Linux.Resources.Memory.Reservation = &reservation } // SetLinuxResourcesMemorySwap sets g.spec.Linux.Resources.Memory.Swap. -func (g Generator) SetLinuxResourcesMemorySwap(swap uint64) { +func (g *Generator) SetLinuxResourcesMemorySwap(swap uint64) { g.spec.Linux.Resources.Memory.Swap = &swap } // SetLinuxResourcesMemoryKernel sets g.spec.Linux.Resources.Memory.Kernel. -func (g Generator) SetLinuxResourcesMemoryKernel(kernel uint64) { +func (g *Generator) SetLinuxResourcesMemoryKernel(kernel uint64) { g.spec.Linux.Resources.Memory.Kernel = &kernel } // SetLinuxResourcesMemoryKernelTCP sets g.spec.Linux.Resources.Memory.KernelTCP. -func (g Generator) SetLinuxResourcesMemoryKernelTCP(kernelTCP uint64) { +func (g *Generator) SetLinuxResourcesMemoryKernelTCP(kernelTCP uint64) { g.spec.Linux.Resources.Memory.KernelTCP = &kernelTCP } // SetLinuxResourcesMemorySwappiness sets g.spec.Linux.Resources.Memory.Swappiness. -func (g Generator) SetLinuxResourcesMemorySwappiness(swappiness uint64) { +func (g *Generator) SetLinuxResourcesMemorySwappiness(swappiness uint64) { g.spec.Linux.Resources.Memory.Swappiness = &swappiness } // ClearLinuxSysctl clears g.spec.Linux.Sysctl. -func (g Generator) ClearLinuxSysctl() { +func (g *Generator) ClearLinuxSysctl() { g.spec.Linux.Sysctl = make(map[string]string) } // AddLinuxSysctl adds a new sysctl config into g.spec.Linux.Sysctl. -func (g Generator) AddLinuxSysctl(s string) error { +func (g *Generator) AddLinuxSysctl(s string) error { if g.spec.Linux.Sysctl == nil { g.spec.Linux.Sysctl = make(map[string]string) } @@ -427,7 +427,7 @@ func (g Generator) AddLinuxSysctl(s string) error { } // RemoveLinuxSysctl removes a sysctl config from g.spec.Linux.Sysctl. -func (g Generator) RemoveLinuxSysctl(key string) { +func (g *Generator) RemoveLinuxSysctl(key string) { if g.spec.Linux.Sysctl == nil { return } @@ -435,7 +435,7 @@ func (g Generator) RemoveLinuxSysctl(key string) { } // SetLinuxSeccompDefault sets g.spec.Linux.Seccomp.DefaultAction. -func (g Generator) SetLinuxSeccompDefault(sdefault string) error { +func (g *Generator) SetLinuxSeccompDefault(sdefault string) error { switch sdefault { case "": case "SCMP_ACT_KILL": @@ -482,7 +482,7 @@ func checkSeccompArch(arch string) error { } // ClearLinuxSeccompArch clears g.spec.Linux.Seccomp.Architectures. -func (g Generator) ClearLinuxSeccompArch() { +func (g *Generator) ClearLinuxSeccompArch() { if g.spec.Linux.Seccomp == nil { return } @@ -491,7 +491,7 @@ func (g Generator) ClearLinuxSeccompArch() { } // AddLinuxSeccompArch adds sArch into g.spec.Linux.Seccomp.Architectures. -func (g Generator) AddLinuxSeccompArch(sArch string) error { +func (g *Generator) AddLinuxSeccompArch(sArch string) error { if err := checkSeccompArch(sArch); err != nil { return err } @@ -506,7 +506,7 @@ func (g Generator) AddLinuxSeccompArch(sArch string) error { } // RemoveSeccompArch removes sArch from g.spec.Linux.Seccomp.Architectures. -func (g Generator) RemoveSeccompArch(sArch string) error { +func (g *Generator) RemoveSeccompArch(sArch string) error { if err := checkSeccompArch(sArch); err != nil { return err } @@ -610,7 +610,7 @@ func parseSeccompSyscall(s string) (rspec.Syscall, error) { } // ClearLinuxSeccompSyscall clears g.spec.Linux.Seccomp.Syscalls. -func (g Generator) ClearLinuxSeccompSyscall() { +func (g *Generator) ClearLinuxSeccompSyscall() { if g.spec.Linux.Seccomp == nil { return } @@ -619,7 +619,7 @@ func (g Generator) ClearLinuxSeccompSyscall() { } // AddLinuxSeccompSyscall adds sSyscall into g.spec.Linux.Seccomp.Syscalls. -func (g Generator) AddLinuxSeccompSyscall(sSyscall string) error { +func (g *Generator) AddLinuxSeccompSyscall(sSyscall string) error { f, err := parseSeccompSyscall(sSyscall) if err != nil { return err @@ -634,7 +634,7 @@ func (g Generator) AddLinuxSeccompSyscall(sSyscall string) error { } // AddLinuxSeccompSyscallAllow adds seccompAllow into g.spec.Linux.Seccomp.Syscalls. -func (g Generator) AddLinuxSeccompSyscallAllow(seccompAllow string) { +func (g *Generator) AddLinuxSeccompSyscallAllow(seccompAllow string) { if g.spec.Linux.Seccomp == nil { g.spec.Linux.Seccomp = &rspec.Seccomp{} } @@ -647,7 +647,7 @@ func (g Generator) AddLinuxSeccompSyscallAllow(seccompAllow string) { } // AddLinuxSeccompSyscallErrno adds seccompErrno into g.spec.Linux.Seccomp.Syscalls. -func (g Generator) AddLinuxSeccompSyscallErrno(seccompErrno string) { +func (g *Generator) AddLinuxSeccompSyscallErrno(seccompErrno string) { if g.spec.Linux.Seccomp == nil { g.spec.Linux.Seccomp = &rspec.Seccomp{} } @@ -661,7 +661,7 @@ func (g Generator) AddLinuxSeccompSyscallErrno(seccompErrno string) { // RemoveSeccompSyscallByName removes all the seccomp syscalls with the given // name from g.spec.Linux.Seccomp.Syscalls. -func (g Generator) RemoveSeccompSyscallByName(name string) error { +func (g *Generator) RemoveSeccompSyscallByName(name string) error { if g.spec.Linux.Seccomp == nil { return nil } @@ -678,7 +678,7 @@ func (g Generator) RemoveSeccompSyscallByName(name string) error { // RemoveSeccompSyscallByAction removes all the seccomp syscalls with the given // action from g.spec.Linux.Seccomp.Syscalls. -func (g Generator) RemoveSeccompSyscallByAction(action string) error { +func (g *Generator) RemoveSeccompSyscallByAction(action string) error { if g.spec.Linux.Seccomp == nil { return nil } @@ -699,7 +699,7 @@ func (g Generator) RemoveSeccompSyscallByAction(action string) error { // RemoveSeccompSyscall removes all the seccomp syscalls with the given // name and action from g.spec.Linux.Seccomp.Syscalls. -func (g Generator) RemoveSeccompSyscall(name string, action string) error { +func (g *Generator) RemoveSeccompSyscall(name string, action string) error { if g.spec.Linux.Seccomp == nil { return nil } @@ -749,12 +749,12 @@ func parseIDMapping(idms string) (rspec.IDMapping, error) { } // ClearLinuxUIDMappings clear g.spec.Linux.UIDMappings. -func (g Generator) ClearLinuxUIDMappings() { +func (g *Generator) ClearLinuxUIDMappings() { g.spec.Linux.UIDMappings = []rspec.IDMapping{} } // AddLinuxUIDMapping adds uidMap into g.spec.Linux.UIDMappings. -func (g Generator) AddLinuxUIDMapping(uidMap string) error { +func (g *Generator) AddLinuxUIDMapping(uidMap string) error { r, err := parseIDMapping(uidMap) if err != nil { return err @@ -765,12 +765,12 @@ func (g Generator) AddLinuxUIDMapping(uidMap string) error { } // ClearLinuxGIDMappings clear g.spec.Linux.GIDMappings. -func (g Generator) ClearLinuxGIDMappings() { +func (g *Generator) ClearLinuxGIDMappings() { g.spec.Linux.GIDMappings = []rspec.IDMapping{} } // AddLinuxGIDMapping adds gidMap into g.spec.Linux.GIDMappings. -func (g Generator) AddLinuxGIDMapping(gidMap string) error { +func (g *Generator) AddLinuxGIDMapping(gidMap string) error { r, err := parseIDMapping(gidMap) if err != nil { return err @@ -781,7 +781,7 @@ func (g Generator) AddLinuxGIDMapping(gidMap string) error { } // SetLinuxRootPropagation sets g.spec.Linux.RootfsPropagation. -func (g Generator) SetLinuxRootPropagation(rp string) error { +func (g *Generator) SetLinuxRootPropagation(rp string) error { switch rp { case "": case "private": @@ -808,43 +808,43 @@ func parseHook(s string) rspec.Hook { } // ClearPreStartHooks clear g.spec.Hooks.Prestart. -func (g Generator) ClearPreStartHooks() { +func (g *Generator) ClearPreStartHooks() { g.spec.Hooks.Prestart = []rspec.Hook{} } // AddPreStartHook add a prestart hook into g.spec.Hooks.Prestart. -func (g Generator) AddPreStartHook(s string) error { +func (g *Generator) AddPreStartHook(s string) error { hook := parseHook(s) g.spec.Hooks.Prestart = append(g.spec.Hooks.Prestart, hook) return nil } // ClearPostStopHooks clear g.spec.Hooks.Poststop. -func (g Generator) ClearPostStopHooks() { +func (g *Generator) ClearPostStopHooks() { g.spec.Hooks.Poststop = []rspec.Hook{} } // AddPostStopHook adds a poststop hook into g.spec.Hooks.Poststop. -func (g Generator) AddPostStopHook(s string) error { +func (g *Generator) AddPostStopHook(s string) error { hook := parseHook(s) g.spec.Hooks.Poststop = append(g.spec.Hooks.Poststop, hook) return nil } // ClearPostStartHooks clear g.spec.Hooks.Poststart. -func (g Generator) ClearPostStartHooks() { +func (g *Generator) ClearPostStartHooks() { g.spec.Hooks.Poststart = []rspec.Hook{} } // AddPostStartHook adds a poststart hook into g.spec.Hooks.Poststart. -func (g Generator) AddPostStartHook(s string) error { +func (g *Generator) AddPostStartHook(s string) error { hook := parseHook(s) g.spec.Hooks.Poststart = append(g.spec.Hooks.Poststart, hook) return nil } // AddTmpfsMount adds a tmpfs mount into g.spec.Mounts. -func (g Generator) AddTmpfsMount(dest string) error { +func (g *Generator) AddTmpfsMount(dest string) error { mnt := rspec.Mount{ Destination: dest, Type: "tmpfs", @@ -857,7 +857,7 @@ func (g Generator) AddTmpfsMount(dest string) error { } // AddCgroupsMount adds a cgroup mount into g.spec.Mounts. -func (g Generator) AddCgroupsMount(mountCgroupOption string) error { +func (g *Generator) AddCgroupsMount(mountCgroupOption string) error { switch mountCgroupOption { case "ro": case "rw": @@ -879,7 +879,7 @@ func (g Generator) AddCgroupsMount(mountCgroupOption string) error { } // AddBindMount adds a bind mount into g.spec.Mounts. -func (g Generator) AddBindMount(bind string) error { +func (g *Generator) AddBindMount(bind string) error { var source, dest string options := "ro" bparts := strings.SplitN(bind, ":", 3) @@ -904,7 +904,7 @@ func (g Generator) AddBindMount(bind string) error { } // SetupPrivileged sets up the priviledge-related fields inside g.spec. -func (g Generator) SetupPrivileged(privileged bool) { +func (g *Generator) SetupPrivileged(privileged bool) { if privileged { // Add all capabilities in privileged mode. var finalCapList []string @@ -936,12 +936,12 @@ func checkCap(c string) error { } // ClearProcessCapabilities clear g.spec.Process.Capabilities. -func (g Generator) ClearProcessCapabilities() { +func (g *Generator) ClearProcessCapabilities() { g.spec.Process.Capabilities = []string{} } // AddProcessCapability adds a process capability into g.spec.Process.Capabilities. -func (g Generator) AddProcessCapability(c string) error { +func (g *Generator) AddProcessCapability(c string) error { if err := checkCap(c); err != nil { return err } @@ -959,7 +959,7 @@ func (g Generator) AddProcessCapability(c string) error { } // DropProcessCapability drops a process capability from g.spec.Process.Capabilities. -func (g Generator) DropProcessCapability(c string) error { +func (g *Generator) DropProcessCapability(c string) error { if err := checkCap(c); err != nil { return err } @@ -998,13 +998,13 @@ func mapStrToNamespace(ns string, path string) (rspec.Namespace, error) { } // ClearLinuxNamespaces clear g.spec.Linux.Namespaces. -func (g Generator) ClearLinuxNamespaces() { +func (g *Generator) ClearLinuxNamespaces() { g.spec.Linux.Namespaces = []rspec.Namespace{} } // AddOrReplaceLinuxNamespace adds or replaces a namespace inside // g.spec.Linux.Namespaces. -func (g Generator) AddOrReplaceLinuxNamespace(ns string, path string) error { +func (g *Generator) AddOrReplaceLinuxNamespace(ns string, path string) error { namespace, err := mapStrToNamespace(ns, path) if err != nil { return err @@ -1021,7 +1021,7 @@ func (g Generator) AddOrReplaceLinuxNamespace(ns string, path string) error { } // RemoveLinuxNamespace removes a namespace from g.spec.Linux.Namespaces. -func (g Generator) RemoveLinuxNamespace(ns string) error { +func (g *Generator) RemoveLinuxNamespace(ns string) error { namespace, err := mapStrToNamespace(ns, "") if err != nil { return err