github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/core/uname/uname.go (about)

     1  // Copyright 2015-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !plan9
     6  // +build !plan9
     7  
     8  // Print build information about the kernel and machine.
     9  //
    10  // Synopsis:
    11  //
    12  //	uname [-asnrvmd]
    13  //
    14  // Options:
    15  //
    16  //	-a: print everything
    17  //	-s: print the kernel name
    18  //	-n: print the network node name
    19  //	-r: print the kernel release
    20  //	-v: print the kernel version
    21  //	-m: print the machine hardware name
    22  //	-d: print your domain name
    23  package main
    24  
    25  import (
    26  	"flag"
    27  	"fmt"
    28  	"log"
    29  	"strings"
    30  
    31  	"golang.org/x/sys/unix"
    32  )
    33  
    34  var (
    35  	all       = flag.Bool("a", false, "print everything")
    36  	kernel    = flag.Bool("s", false, "print the kernel name")
    37  	node      = flag.Bool("n", false, "print the network node name")
    38  	release   = flag.Bool("r", false, "print the kernel release")
    39  	version   = flag.Bool("v", false, "print the kernel version")
    40  	machine   = flag.Bool("m", false, "print the machine hardware name")
    41  	processor = flag.Bool("p", false, "print the machine hardware name")
    42  	domain    = flag.Bool("d", false, "print your domain name")
    43  )
    44  
    45  func handleFlags(u *unix.Utsname) string {
    46  	Sysname, Nodename := unix.ByteSliceToString(u.Sysname[:]), unix.ByteSliceToString(u.Nodename[:])
    47  	Release, Version := unix.ByteSliceToString(u.Release[:]), unix.ByteSliceToString(u.Version[:])
    48  	Machine, Domainname := unix.ByteSliceToString(u.Machine[:]), unix.ByteSliceToString(u.Domainname[:])
    49  	info := make([]string, 0, 6)
    50  
    51  	if *all || flag.NFlag() == 0 {
    52  		info = append(info, Sysname, Nodename, Release, Version, Machine, Domainname)
    53  		goto end
    54  	}
    55  	if *kernel {
    56  		info = append(info, Sysname)
    57  	}
    58  	if *node {
    59  		info = append(info, Nodename)
    60  	}
    61  	if *release {
    62  		info = append(info, Release)
    63  	}
    64  	if *version {
    65  		info = append(info, Version)
    66  	}
    67  	if *machine || *processor {
    68  		info = append(info, Machine)
    69  	}
    70  	if *domain {
    71  		info = append(info, Domainname)
    72  	}
    73  
    74  end:
    75  	return strings.Join(info, " ")
    76  }
    77  
    78  func main() {
    79  	flag.Parse()
    80  
    81  	var u unix.Utsname
    82  	if err := unix.Uname(&u); err != nil {
    83  		log.Fatalf("%v", err)
    84  	}
    85  
    86  	info := handleFlags(&u)
    87  	fmt.Println(info)
    88  }