Skip to content
Snippets Groups Projects
Select Git revision
  • 10f086b322119b076febc17eabe26b347dcc9f38
  • main default protected
  • update_2024
  • fix_formatting
4 results

main.rs

Blame
  • Forked from orestis.malaspin / rust-101
    Source project has a limited visibility.
    vmCred2pdf.go 2.54 KiB
    package cmdVM
    
    import (
    	"strconv"
    	u "nexus-client/utils"
    	"github.com/go-pdf/fpdf"
    )
    
    type Cred2pdf struct {
        Name string
    }
    
    func (cmd *Cred2pdf)GetName() string {
    	return cmd.Name
    }
     
    func (cmd *Cred2pdf)GetDesc() string {
    	return "Create a PDF with the credentials required to attach to running VMs (regex matching)."
    }
    
    func (cmd *Cred2pdf)PrintUsage() {
    	u.PrintlnErr(cmd.GetDesc())
    	u.PrintlnErr("Usage: ",cmd.GetName()," [ID ...] [regex ...] pdfile")
    	u.PrintlnErr("Only VMs that can be attached to and matching the specified IDs or regexes will be listed.")
    	const usage string = `Any number of IDs or regexes can be specified.
    The regex only matches the VM's name and is case-insensitive.
    Regex examples:
    ""    -> matches any VMs
    "."   -> matches any VMs
    "bla" -> matches any VMs containing "bla" in their name`
    	u.PrintlnErr(usage)
    }
    
    func (cmd *Cred2pdf)Run(args []string) int {
    	argc := len(args)
    	if argc < 2 {
    		cmd.PrintUsage()
    		return 1
    	}
    
    	pdfFile := args[argc-1]
    
    	vms, err := getFilteredVMs("/vms/attach", args[:argc-1])
    	if err != nil {
    		u.PrintlnErr("Error: "+err.Error())
    		return 1
    	}
    
    	const leftMargin = 4.
    	const topMargin = 4.
    	const rightMargin = 0.
    	columnWidth := [...]float64 {80.,13.,40.,20.,69.}
    	const rowHeight = 15.
    	const fontSize = 10.
    	const cellBorder = "1"  // full cell border; use "" for no border
    	const nextPos = 0       // 0 means to move right
    	const align = "L"
    	const bkgd = false      // transparent background; use true for filled
    	const link = 0          // no link
    	const url = ""          // no url
    	
    	pdf := fpdf.New("p", "mm", "A4", "")  // "p" means portrait orienation
    	pdf.SetMargins(leftMargin, topMargin, rightMargin)
    	pdf.AddPage()
    
    	for _, vm := range vms {
    		pdf.SetX(leftMargin)
    		pdf.SetFont("Arial", "b", fontSize)   // "b" means bold; use "" for normal
    		pdf.CellFormat(columnWidth[0], rowHeight, vm.Name, cellBorder, nextPos, align, bkgd, link, url)
    		pdf.SetFont("Courier", "b", fontSize)   // "b" means bold; use "" for normal
    		pdf.CellFormat(columnWidth[1], rowHeight, strconv.Itoa(vm.Run.Port), cellBorder, nextPos, align, bkgd, link, url)
    		pdf.SetFont("Courier", "b", fontSize+1)   // "b" means bold; use "" for normal
    		pdf.CellFormat(columnWidth[2], rowHeight, vm.Run.Pwd, cellBorder, nextPos, align, bkgd, link, url)
    		pdf.CellFormat(columnWidth[4], rowHeight, "", cellBorder, nextPos, align, bkgd, link, url)
    		pdf.Ln(-1)  // line break; -1 means move by the height of the last printed cell
    	}
    
    	if err = pdf.OutputFileAndClose(pdfFile); err != nil {
    		u.PrintlnErr("Error: "+err.Error())
    		return 1
    	}
    
    	return 0
    }