gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/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  // Print build information about the kernel and machine.
     6  //
     7  // Synopsis:
     8  //     uname [-asnrvmd]
     9  //
    10  // Options:
    11  //     -a: print everything
    12  //     -s: print the kernel name
    13  //     -n: print the network node name
    14  //     -r: print the kernel release
    15  //     -v: print the kernel version
    16  //     -m: print the machine hardware name
    17  //     -d: print your domain name
    18  package main
    19  
    20  import (
    21  	"bytes"
    22  	"flag"
    23  	"fmt"
    24  	"log"
    25  	"strings"
    26  
    27  	"golang.org/x/sys/unix"
    28  )
    29  
    30  var (
    31  	all       = flag.Bool("a", false, "print everything")
    32  	kernel    = flag.Bool("s", false, "print the kernel name")
    33  	node      = flag.Bool("n", false, "print the network node name")
    34  	release   = flag.Bool("r", false, "print the kernel release")
    35  	version   = flag.Bool("v", false, "print the kernel version")
    36  	machine   = flag.Bool("m", false, "print the machine hardware name")
    37  	processor = flag.Bool("p", false, "print the machine hardware name")
    38  	domain    = flag.Bool("d", false, "print your domain name")
    39  )
    40  
    41  func toString(d []byte) string {
    42  	return string(d[:bytes.IndexByte(d[:], 0)])
    43  }
    44  
    45  func handleFlags(u *unix.Utsname) string {
    46  	Sysname, Nodename := toString(u.Sysname[:]), toString(u.Nodename[:])
    47  	Release, Version := toString(u.Release[:]), toString(u.Version[:])
    48  	Machine, Domainname := toString(u.Machine[:]), toString(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  }