test-client.tcl at tip

File tsmf/test-client.tcl from the latest check-in


#! /usr/bin/env tclsh

lappend auto_path [file join [file dirname [info script]] lib]
lappend auto_path [file join [file dirname [info script]] ..]
package require tsmf

proc maxlen {list} {
	set maxlen 0
	foreach item $list {
		set len [string length $item]
		if {$len > $maxlen} {
			set maxlen $len
		}
	}
	return $maxlen
}

proc relTime {time} {
	set now [clock seconds]
	set delta [expr {$now - $time}]

	set future " "
	if {$delta < 0} {
		set future "+"
		set delta [expr {abs($delta)}]
	}

	set fmt1 "%b_%d"
	set fmt2 "%H:%M:%S"
	set now_fmt1 [clock format $now -format $fmt1]
	set time_fmt [clock format $time -format $fmt1]

	if {$now_fmt1 eq $time_fmt} {
		set time_fmt [clock format $time -format $fmt2]
	}

	return ${future}$time_fmt
}

namespace eval ::cmd {}
proc ::cmd::list {args} {
	set info [::tsmf::client::list_svcs]

	set statelen [expr {[maxlen {starting started stopping stopped}] + 1}]

	set fmt "%-${statelen}s  %10s  %s"

	puts [format $fmt "STATE" "STIME" "SVC"]
	foreach {name svcInfo} $info {
		set state [dict get $svcInfo state]
		set desired_state [dict get $svcInfo desired_state]
		set changed_time [dict get $svcInfo changed_time]
		if {$state ne $desired_state} {
			set state "${state}*"
		}

		puts [format $fmt $state [relTime $changed_time] $name]
	}
}

proc ::cmd::start name {
	::tsmf::client::start $name
}

proc ::cmd::stop name {
	::tsmf::client::stop $name
}

proc ::cmd::info name {
	set info [::tsmf::client::status $name]
	puts $info
}


set mode "list"

if {[llength $argv] > 0} {
	set mode [lindex $argv 0]
	set argv [lrange $argv 1 end]
}

::cmd::$mode $argv