github.com/scaleway/scaleway-cli@v1.11.1/pkg/commands/logs.go (about)

     1  // Copyright (C) 2015 Scaleway. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE.md file.
     4  
     5  package commands
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/scaleway/scaleway-cli/pkg/api"
    11  	"github.com/scaleway/scaleway-cli/pkg/utils"
    12  )
    13  
    14  // LogsArgs are flags for the `RunLogs` function
    15  type LogsArgs struct {
    16  	Gateway string
    17  	Server  string
    18  	SSHUser string
    19  	SSHPort int
    20  }
    21  
    22  // RunLogs is the handler for 'scw logs'
    23  func RunLogs(ctx CommandContext, args LogsArgs) error {
    24  	serverID, err := ctx.API.GetServerID(args.Server)
    25  	if err != nil {
    26  		return err
    27  	}
    28  	server, err := ctx.API.GetServer(serverID)
    29  	if err != nil {
    30  		return fmt.Errorf("failed to get server information for %s: %v", serverID, err)
    31  	}
    32  
    33  	// FIXME: switch to serial history when API is ready
    34  
    35  	// Resolve gateway
    36  	if args.Gateway == "" {
    37  		args.Gateway = ctx.Getenv("SCW_GATEWAY")
    38  	}
    39  	var gateway string
    40  	if args.Gateway == serverID || args.Gateway == args.Server {
    41  		gateway = ""
    42  	} else {
    43  		gateway, err = api.ResolveGateway(ctx.API, args.Gateway)
    44  		if err != nil {
    45  			return fmt.Errorf("cannot resolve Gateway '%s': %v", args.Gateway, err)
    46  		}
    47  	}
    48  
    49  	command := []string{"dmesg"}
    50  	err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args.SSHUser, args.SSHPort, command, true, gateway)
    51  	if err != nil {
    52  		return fmt.Errorf("command execution failed: %v", err)
    53  	}
    54  	return nil
    55  }